/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getShared('eventsManager')); $dispatcher->setDefaultNamespace('App\\Modules\\Oauth\\'); return $dispatcher; }); /** * Module specific database connection */ $di->set('db', function () use($appConfig) { return new DbAdapter(['host' => $moduleConfig->database->host, 'username' => $moduleConfig->database->username, 'password' => $moduleConfig->database->password, 'dbname' => $moduleConfig->database->name]); }); }
public function registerServices(DiInterface $di) { global $config; $di->setShared('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->backend->baseUri); return $url; }); $di->setShared('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers"); return $dispatcher; }); $di->setShared('view', function () use($config) { $view = new View(); $view->setViewsDir($config->backend->viewsDir); $view->setLayoutsDir('layouts/'); $view->setPartialsDir('partials/'); $view->registerEngines(array('.phtml' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->backend->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.volt' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }); }
function setDi() { $di = new FactoryDefault(); $di['router'] = function () use($di) { $router = new Router(); $router->setDefaultModule('mobimall'); return $router; }; $di['url'] = function () { $url = new UrlResolver(); $url->setBaseUri('/'); return $url; }; $di['session'] = function () { $session = new SessionAdapter(); // $session->start(); return $session; }; $loader = new Loader(); $loader->registerNamespaces(array('Mall\\Mdu' => __DIR__ . '/../apps/mdu')); $sysConfig = (include __DIR__ . '/../config/sysconfig.php'); $di['sysconfig'] = function () use($sysConfig) { return $sysConfig; }; $loader->register(); return $di; }
/** * Register the services here to make them general * or register in the ModuleDefinition to make them module-specific */ public function registerServices(DiInterface $di) { //Read configuration $config = (include __DIR__ . "/config/config.php"); // The URL component is used to generate all kind of urls in the application $di->set('url', function () use($config) { $url = new Url(); $url->setBaseUri($config->application->baseUri); return $url; }); //Registering a dispatcher $di->set('dispatcher', function () { //Create/Get an EventManager $eventsManager = new EventsManager(); //Attach a listener $eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(['module' => 'backend', 'controller' => 'errors', 'action' => 'notFound']); return false; } } }); $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers"); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); /** * Setting up the view component */ $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]); $view->registerEngines(['.volt' => 'volt']); // Create an event manager $eventsManager = new EventsManager(); // Attach a listener for type 'view' $eventsManager->attach('view', function ($event, $view) { if ($event->getType() == 'notFoundView') { throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')'); } }); // Bind the eventsManager to the view component $view->setEventsManager($eventsManager); return $view; }); $configMenu = (include __DIR__ . "/config/config.menu.php"); $di->setShared('menuStruct', function () use($configMenu) { // if structure received from db table instead getting from $config // we need to store it to cache for reducing db connections $struct = $configMenu->get('menuStruct')->toArray(); return $struct; }); }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $url = new UrlResolver(); $url->setBaseUri($di->get('config')->application->baseUri); return $url; }, true); }
public function attachUrlResolver($baseUri = '/') { $this->_di->setShared('url', function () use($baseUri) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($baseUri); return $url; }); return $this; }
/** * Register services used by the backend application * * @param $di */ public function registerServices($di) { $config = $this->config(); /** * register the dispatcher */ $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); /*$eventManager = new Manager(); $eventManager->attach('dispatch', new \Acl('backend')); $dispatcher->setEventsManager($eventManager);*/ $dispatcher->setDefaultNamespace('app\\backend\\controllers'); return $dispatcher; }); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }, true); $di->setShared('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(['.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(['compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_']); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']); return $view; }); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { return new MysqlAdapter($config->database->toArray()); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () { return new MetaDataAdapter(); }); /** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); }
public function onBoot() { // TODO: Implement onBoot() method. $this->getDI()->setShared('url', function () { $protocol = stripos(server('SERVER_PROTOCOL'), 'https') === true ? 'https://' : 'http://'; $hostname = server('HTTP_HOST'); $url = new Url(); $url->setStaticBaseUri(env('static_url', "{$protocol}{$hostname}/")); $url->setBaseUri(env('base_url', '/')); return $url; }); }
/** * Registers services related to the module * * @param DiInterface $di */ public function registerServices(DiInterface $di) { /** * Read configuration */ $config = (include APP_PATH . "/apps/backend/config/config.php"); /** * Setting up the view component */ $di['view'] = function () use($config) { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => __DIR__ . '/cache/', 'compiledSeparator' => '_')); $compiler = $volt->getCompiler(); // format number $compiler->addFilter('number', function ($resolvedArgs) { return 'Helpers::number(' . $resolvedArgs . ');'; }); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }; /** * Database connection is created based in the parameters defined in the configuration file */ $di['db'] = function () use($config) { $config = $config->database->toArray(); $dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter']; unset($config['adapter']); return new $dbAdapter($config); }; /** * Logger service */ $di->set('logger', function ($filename = null, $format = null) use($config) { $format = $format ?: $config->get('logger')->format; $filename = trim($filename ?: $config->get('logger')->filename, '\\/'); $path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR; $formatter = new FormatterLine($format, $config->get('logger')->date); $logger = new FileLogger($path . $filename); $logger->setFormatter($formatter); $logger->setLogLevel($config->get('logger')->logLevel); return $logger; }); $di->set('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri("/backend/"); return $url; }); }
/** * Initializes the response object and returns it * * @author Nikolaos Dimopoulos <*****@*****.**> * @since 2014-10-05 * * @return PhTResponse */ protected function getResponseObject() { PhDI::reset(); $di = new PhDI(); $di->set('url', function () { $url = new PhUrl(); $url->setBaseUri('/'); return $url; }); $response = new PhTResponse(); $response->setDI($di); return $response; }
/** * Set url service when module start * @param Di $di * @param Config $config * @param string $name Module name * @return \Phalex\Events\Listener\Application */ private function setUrlService(Di $di, Config $config, $name) { $base = $static = '/'; if (isset($config['url'])) { $default = isset($config['url']['default']) ? $config['url']['default'] : '/'; if (isset($config['url'][$name])) { $base = isset($config['url'][$name]['uri']) ? $config['url'][$name]['uri'] : $default; $static = isset($config['url'][$name]['static']) ? $config['url'][$name]['static'] : $default; } } $url = new UrlService(); $url->setBaseUri($base); $url->setStaticBaseUri($static); $di->set('url', $url, true); return $this; }
/** * This method is called before a test is executed. */ protected function setUp() { $this->checkExtension('phalcon'); // Reset the DI container Di::reset(); // Instantiate a new DI container $di = new Di(); // Set the URL $di->set('url', function () { $url = new Url(); $url->setBaseUri('/'); return $url; }); $di->set('escaper', function () { return new Escaper(); }); $this->di = $di; }
/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getShared('eventsManager')); $dispatcher->setDefaultNamespace('App\\Modules\\Frontend\\'); return $dispatcher; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Include config per environment */ include __DIR__ . '/config/config_' . $appConfig->application->environment . '.php'; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("country_code"); /** * Simple database connection to localhost */ $di->setShared('mongo', function ($config, $database) { $mongo = new \Mongo(); return $mongo->selectDb($config->{$database}->dbname); }, true); $di->setShared('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }, true); }
public function testGet() { $url = new Url(); $url->setBaseUri('http://www.test.com'); $this->assertEquals('http://www.test.com', $url->get('')); $this->assertEquals('http://www.test.com/', $url->get('/')); $this->assertEquals('http://www.test.com/path', $url->get('/path')); $url->setBaseUri('http://www.test.com/?_url=/'); $this->assertEquals('http://www.test.com/?_url=/path¶ms=one', $url->get('path', array('params' => 'one'))); }
/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * Setting up the view component */ $di->set('view', function () { $view = new View(); $view->setViewsDir(__DIR__ . '/../../../public/src/app/modules/leads/views/')->setLayoutsDir('../../../layouts/')->setPartialsDir('../../../partials/')->setTemplateAfter('main')->registerEngines(['.html' => 'Phalcon\\Mvc\\View\\Engine\\Php']); return $view; }); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getShared('eventsManager')); $dispatcher->setDefaultNamespace('App\\Modules\\Leads\\'); return $dispatcher; }); /** * Module specific database connection */ $di->set('db', function () use($appConfig) { return new DbAdapter(['host' => $moduleConfig->database->host, 'username' => $moduleConfig->database->username, 'password' => $moduleConfig->database->password, 'dbname' => $moduleConfig->database->name]); }); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ public function registerServices(\Phalcon\DiInterface $di) { /** * Read configuration */ $config = (include __DIR__ . "/config/config.php"); /** * Setting up the view component */ // The URL component is used to generate all kind of urls in the application $di->set('url', function () { $url = new Url(); $url->setBaseUri('/'); return $url; }); /** * Setting up the view component */ $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->view->viewsDir); $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]); $view->registerEngines(['.volt' => function () use($view, $config) { $volt = new Volt($view); $volt->setOptions(['compiledPath' => $config->application->view->compiledPath, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compiledExtension' => $config->application->view->compiledExtension, 'compileAlways' => true]); return $volt; }]); // Create an event manager $eventsManager = new EventsManager(); // Attach a listener for type 'view' $eventsManager->attach('view', function ($event, $view) { if ($event->getType() == 'notFoundView') { throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')'); } }); // Bind the eventsManager to the view component $view->setEventsManager($eventsManager); return $view; }); }
/** * Sets the test up by loading the DI container and other stuff * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-09-30 * @param \Phalcon\DiInterface $di * @param \Phalcon\Config $config * @return void */ protected function setUp(DiInterface $di = null, Config $config = null) { $this->checkExtension('phalcon'); if (!is_null($config)) { $this->config = $config; } if (is_null($di)) { // Reset the DI container DI::reset(); // Instantiate a new DI container $di = new FactoryDefault(); // Set the URL $di->set('url', function () { $url = new Url(); $url->setBaseUri('/'); return $url; }); $di->set('escaper', function () { return new \Phalcon\Escaper(); }); } $this->di = $di; }
private function setUrl() { $url = new Url(); if (!$this->application->debug) { $url->setBaseUri($this->application->production->baseUri); $url->setStaticBaseUri($this->application->production->staticBaseUri); } else { $url->setBaseUri($this->application->development->baseUri); $url->setStaticBaseUri($this->application->development->staticBaseUri); } return $url; }
/** * @param null $uri * @param null $args * @return string */ public function get($uri = null, $args = null, $local = null) { if (isset($uri['for'])) { $lang = null; if (isset($uri[LangRouter::LANG_PARAM])) { $lang = $uri[LangRouter::LANG_PARAM]; } $route = $this->getDI()->getRouter()->getRouteByName($uri['for'], $lang); if ($route === null) { return "notfound"; } else { $paths = $route->getPaths(); $request = $this->getDI()->getRequest(); $dispatcher = $this->getDI()->getDispatcher(); foreach ($paths as $pathKey => $value) { if (!array_key_exists($pathKey, $uri) && $pathKey !== 'for' && $pathKey) { $uri[$pathKey] = $dispatcher->getParam($pathKey) ?: $value; } } // if query args should be included if (!isset($uri['type']) || $uri['type'] !== 'no_query') { if ($args === null) { $args = []; } if ($uri['for'] === 'this') { foreach ($request->get() as $param => $value) { if (!array_key_exists($param, $args) && $param !== '_url' && $param !== 'setLang') { $args[$param] = $value; } } } } if ($lang !== null) { $uri["for"] = $route->getName(); } } } return parent::get($uri, $args); }
<?php use Phalcon\DI\FactoryDefault; use Phalcon\Mvc\View; use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; use Phalcon\Mvc\View\Engine\Volt as VoltEngine; use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter; use Phalcon\Session\Adapter\Files as SessionAdapter; $di = new FactoryDefault(); $di->set('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }, true); $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_', 'compileAlways' => true)); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true); $di->set('db', function () use($config) { return new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname, "charset" => $config->database->charset)); }); $di->set('modelsMetadata', function () { return new MetaDataAdapter(); });
}, true );*/ //Github API mock $di->set('github', function () use($config) { $api = m::mock("api"); $api->shouldReceive("show")->with("simple-helpers", "php-file-mover", 8)->andReturn(json_decode(file_get_contents(__DIR__ . "/../_data/8"), true)); $client = m::mock("Github"); $client->shouldReceive("api")->with('pull_request')->andReturn($api); return $client; }, true); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($config) { $url = new UrlResolver(); if (!$config->application->debug) { $url->setBaseUri($config->application->production->baseUri); $url->setStaticBaseUri($config->application->production->staticBaseUri); } else { $url->setBaseUri($config->application->development->baseUri); $url->setStaticBaseUri($config->application->development->staticBaseUri); } return $url; }, true); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { $connection = new DatabaseConnection($config->database->toArray()); $debug = $config->application->debug;
public function path($path = null) { return parent::path($path); }
/** * @ticket 1960 * @author Vladimir Kolesnikov <*****@*****.**> * @since 2014-02-02 */ public function testIssue1960() { $url = new \Phalcon\Mvc\Url(); $url->setDI($this->di); $params = 'http://www.google.com/'; $expected = 'http://www.google.com/'; $actual = $url->get($params); $this->assertEquals($expected, $actual, 'External Site Url not correct'); }
use Phalcon\Di\FactoryDefault; use Phalcon\Mvc\Url as UrlProvider; use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter; try { // Register an autoloader $loader = new Loader(); $loader->registerDirs(array('../app/controllers/', '../app/models/'))->register(); // Setup the database service $di->set('db', function () { return new DbAdapter(array("host" => "pellefant-01.db.elephantsql.com", "username" => "yxlluzxv", "password" => "XlspU6IwYJrlKnn9drdaRfpRv2PjyFR5", "dbname" => "yxlluzxv")); }); // Create a DI $di = new FactoryDefault(); // Setup the view component $di->set('view', function () { $view = new View(); $view->setViewsDir('../app/views/'); return $view; }); // Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function () { $url = new UrlProvider(); $url->setBaseUri(''); return $url; }); // Handle the request $application = new Application($di); echo $application->handle()->getContent(); } catch (\Exception $e) { echo "Exception: ", $e->getMessage(); }
/** * Tests the url with external website * * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-11-29 */ public function testUrlForExternalSite() { $url = new PhUrl(); $url->setDI($this->di); $params = array('for' => 'wikipedia', 'article' => 'Television_news'); $expected = '/wiki/Television_news'; $actual = $url->get($params); $this->assertEquals($expected, $actual, 'External Site Url not correct'); }
use Phalcon\DI\FactoryDefault; use Phalcon\Session\Adapter\Files as SessionAdapter; /** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ $di = new FactoryDefault(); /** * Registering a router */ $di['router'] = function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->setDefaultNamespace("@@namespace@@\\Frontend\\Controllers"); return $router; }; /** * The URL component is used to generate all kind of urls in the application */ $di['url'] = function () { $url = new UrlResolver(); $url->setBaseUri('/@@name@@/'); return $url; }; /** * Start the session the first time some component request the session service */ $di['session'] = function () { $session = new SessionAdapter(); $session->start(); return $session; };
/** * Initializes the baseUrl * * @param array $options */ protected function initUrl($options = array()) { $config = $this->_di->get('config'); /** * The URL component is used to generate all kind of urls in the * application */ $this->_di->set('url', function () use($config) { $url = new PhUrl(); $url->setBaseUri($config->app->baseUri); return $url; }); }
try { // Register an autoloader $loader = new Loader(); $loader->registerDirs(['../app/controllers/', '../app/models/'])->register(); // Create a DI $di = new FactoryDefault(); // Set the database service $di['db'] = function () { return new DbAdapter(["host" => "localhost", "username" => "root", "password" => "pass", "dbname" => "jdi-cars"]); }; // Setting up the view component $di['view'] = function () { $view = new View(); $view->setViewsDir('../app/views/'); return $view; }; $di['url'] = function () { $url = new Url(); $url->setBaseUri('/'); return $url; }; // Setup the tag helpers $di['tag'] = function () { return new Tag(); }; // Handle the request $application = new Application($di); echo $application->handle()->getContent(); } catch (Exception $e) { echo "Exception: ", $e->getMessage(); }
use Phalcon\Mvc\View; use Phalcon\Mvc\View\Engine\Volt as VoltEngine; use Phalcon\Session\Adapter\Files as SessionAdapter; /** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ $di = new FactoryDefault(); /** * Set router for rewrite url */ $di->setShared('router', include __DIR__ . '/routers.php'); /** * The URL component is used to generate all kind of urls in the application */ $di->setShared('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); $url->setStaticBaseUri($config->application->staticUri); return $url; }); /** * Setting up the view component */ $di->setShared('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '.')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->setShared('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); }