/** * Initializes router system */ public function init() { $di = $this->getDi(); $eventsManager = $this->getEventsManager(); $routerCacheKey = 'router_data.cache'; $cacheData = $di->get('cacheData'); $router = $cacheData->get($routerCacheKey); if ($this->_config->application->debug || $router === null) { $saveToCache = $router === null; // load all controllers of all modules for routing system $modules = $di->get('modules'); //Use the annotations router $defaultModule = $this->_config->application->defaultModule; $router = new \Phalcon\Mvc\Router\Annotations(false); $router->removeExtraSlashes(true); $router->setDefaultModule($defaultModule); $router->setDefaultNamespace(ucfirst($defaultModule) . '\\Controller'); $router->setDefaultController("index"); $router->setDefaultAction("index"); $router->add('/:module/:controller/:action', ['module' => 1, 'controller' => 2, 'action' => 3]); $router->add('/:controller/:action', ['controller' => 1, 'action' => 2]); $router->add('/:controller', ['controller' => 1]); $router->notFound(['module' => $defaultModule, 'namespace' => ucfirst($defaultModule) . '\\Controller', 'controller' => 'error', 'action' => 'show404']); //Read the annotations from controllers foreach ($modules as $module => $enabled) { if (!$enabled) { continue; } if (!file_exists($this->_config->application->modulesDir . ucfirst($module) . '/Controller')) { continue; } $files = scandir($this->_config->application->modulesDir . ucfirst($module) . '/Controller'); // get all file names foreach ($files as $file) { // iterate files if ($file == "." || $file == "..") { continue; } $controller = ucfirst($module) . '\\Controller\\' . str_replace('Controller.php', '', $file); if (strpos($file, 'Controller.php') !== false) { $router->addModuleResource(strtolower($module), $controller); } } } if ($saveToCache) { $cacheData->save($routerCacheKey, $router, 3600); // 30 days cache } } $di->set('router', $router); }
<?php $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); $di->setShared('router', function () use($di) { $router = new \Phalcon\Mvc\Router\Annotations(false); $router->setDefaultModule('frontend'); $router->setDefaultController('Application\\Frontend\\Controllers\\Default'); $router->setDefaultAction('home'); $resources = array('backend' => array('Application\\Backend\\Controller\\User'), 'common' => array('Application\\Common\\Controller\\Test', 'Application\\Common\\Controller\\Locale', 'Application\\Common\\Controller\\Error'), 'frontend' => array('Application\\Frontend\\Controller\\Default', 'Application\\Frontend\\Controller\\User')); foreach ($resources as $module => $controllers) { foreach ($controllers as $controller) { $router->addModuleResource($module, $controller); } } if ($di instanceof \Phalcon\DI\FactoryDefault\CLI) { $router = new \Phalcon\Cli\Router(); } return $router; });