Ejemplo n.º 1
0
 /**
  * Init router.
  *
  * @param 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);
         $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);
         $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;
 }
Ejemplo n.º 2
0
 /**
  * Init router.
  *
  * @param DI     $di     Dependency Injection.
  * @param Config $config Config object.
  *
  * @return Router
  */
 protected function _initRouter($di, $config)
 {
     $cacheData = $di->get('cacheData');
     $router = $cacheData->get(EnSystem::CACHE_KEY_ROUTER_DATA);
     if ($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 PhRouterAnnotations(false);
         $router->setDefaultModule(Application::SYSTEM_DEFAULT_MODULE);
         $router->setDefaultNamespace(ucfirst(Application::SYSTEM_DEFAULT_MODULE) . '\\Controller');
         $router->setDefaultController('Index');
         $router->setDefaultAction('index');
         /**
          * Load all route from router file
          */
         foreach ($modules as $module) {
             $moduleName = ucfirst($module);
             // Get all file names.
             $moduleDir = opendir($di->get('registry')->directories->modules . $moduleName . '/Controller');
             while ($file = readdir($moduleDir)) {
                 if ($file == "." || $file == ".." || strpos($file, 'Controller.php') === false) {
                     continue;
                 }
                 $controller = $moduleName . '\\Controller\\' . str_replace('Controller.php', '', $file);
                 $router->addModuleResource(strtolower($module), $controller);
             }
             closedir($moduleDir);
         }
         if ($saveToCache) {
             $cacheData->save(EnSystem::CACHE_KEY_ROUTER_DATA, $router, 2592000);
             // 30 days cache
         }
     }
     $router->removeExtraSlashes(true);
     $di->set('router', $router);
     return $router;
 }
Ejemplo n.º 3
0
 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;
     });
 }