public function boot(Container $container) { // Pails 不使用Phalcon的Module功能,通过Namespace组织Controllers. // 如果出现多级,比如AdminApi,则一定要以Namespace的形式组织 Admin\Api\xxxxController // // Note: from phalcon 3, closure bind di as $this by default. so no use($app) needed. // $container->setShared('router', function () { // $router = new Annotations(false); $router->removeExtraSlashes(true); $router->setEventsManager($this->get('eventsManager')); $router->setDefaultNamespace('App\\Controllers'); $router->setDefaultController('application'); $router->setDefaultAction('index'); // Process /config/routes.php // Verb Path Action Route Name // GET /photo index photo.index // GET /photo/create create photo.create // POST /photo store photo.store // GET /photo/{photo} show photo.show // GET /photo/{photo}/edit edit photo.edit // PUT/PATCH /photo/{photo} update photo.update // DELETE /photo/{photo} destroy photo.destroy foreach ($this->getConfig('routes') as $url => $route) { // $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']; // // // a RESTful resource // if (isset($route['resource'])) { // if (!isset($route[''])) {} // foreach ($this->getResourceMethods($defaults, $options) as $m) { // $this->{'addResource'.ucfirst($m)}($url, $route, $options); // } // } else { // if (count($route) !== count($route, COUNT_RECURSIVE)) { // if (isset($route['pattern']) && isset($route['paths'])) { // $method = isset($route['httpMethods']) ? $route['httpMethods'] : null; // $router->add($route['pattern'], $route['paths'], $method); // } else { // throw new \RuntimeException(sprintf('No route pattern and paths found by route %s', $url)); // } // } else { // $router->add($url, $route); // } // } } // 定义注解路由 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path('Controllers')), \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { if (Text::endsWith($item, "Controller.php", false)) { $name = str_replace([$this->path('Controllers') . DIRECTORY_SEPARATOR, "Controller.php"], "", $item); $name = str_replace(DIRECTORY_SEPARATOR, "\\", $name); $router->addResource('App\\Controllers\\' . $name); } } // 定义404路由 $router->notFound(["controller" => "application", "action" => "notfound"]); // return $router; }); }
use Phalcon\Mvc\View; use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Mvc\View\Engine\Volt as VoltEngine; use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter; use Phalcon\Session\Adapter\Files as SessionAdapter; use Phalcon\Flash\Direct as Flash; use Phalcon\Mvc\Dispatcher; $di = new FactoryDefault(); $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("QaApp\\Controllers"); return $dispatcher; }); $di->set('router', function () { $router = new Annotations(false); $router->removeExtraSlashes(true); //phalcon bug => fixed in 2.1.x //$router->setDefaultNamespace("QaApp\\Controllers"); $router->addResource("QaApp\\Controllers\\Question", '/v1/questions'); return $router; }); $di->setShared('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }); $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);
/** * Init router. * * @param DIBehaviour|DI $di Dependency Injection. * @param Config $config Config object. * * @return Router */ protected function _initRouter($di, $config) { $defaultModuleName = ucfirst(Application::SYSTEM_DEFAULT_MODULE); // Check installation. if (!$config->installed) { $router = new RouterAnnotations(false); // Use $_SERVER['REQUEST_URI'] (NGINX) if (!isset($_GET['_url'])) { $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); // Remove extra slashes from url $router->removeExtraSlashes(true); } $router->setDefaultModule(Application::SYSTEM_DEFAULT_MODULE); $router->setDefaultNamespace($defaultModuleName . '\\Controller'); $router->setDefaultController("Install"); $router->setDefaultAction("index"); $router->addModuleResource(Application::SYSTEM_DEFAULT_MODULE, $defaultModuleName . '\\Controller\\Install'); $di->set('installationRequired', true); $di->set('router', $router); return; } $cacheData = $di->get('cacheData'); $router = $cacheData->get(System::CACHE_KEY_ROUTER_DATA); if ($config->application->debug || $router === null) { $saveToCache = $router === null; // Load all controllers of all modules for routing system. $modules = $di->get('registry')->modules; // Use the annotations router. $router = new RouterAnnotations(true); // Use $_SERVER['REQUEST_URI'] (NGINX) if (!isset($_GET['_url'])) { $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); // Remove extra slashes from url $router->removeExtraSlashes(true); } $router->setDefaultModule(Application::SYSTEM_DEFAULT_MODULE); $router->setDefaultNamespace(ucfirst(Application::SYSTEM_DEFAULT_MODULE) . '\\Controller'); $router->setDefaultController("Index"); $router->setDefaultAction("index"); // Read the annotations from controllers. foreach ($modules as $module) { $moduleName = ucfirst($module); // Get all file names. $files = scandir($di->get('registry')->directories->modules . $moduleName . '/Controller'); // Iterate files. foreach ($files as $file) { if ($file == "." || $file == ".." || strpos($file, 'Controller.php') === false) { continue; } $controller = $moduleName . '\\Controller\\' . str_replace('Controller.php', '', $file); $router->addModuleResource(strtolower($module), $controller); } } if ($saveToCache) { $cacheData->save(System::CACHE_KEY_ROUTER_DATA, $router, 2592000); // 30 days cache } } $di->set('router', $router); return $router; }