Exemplo n.º 1
0
 /**
  * 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);
 }
 public function testRouterFullResources()
 {
     $router = new Phalcon\Mvc\Router\Annotations(false);
     $router->setDI($this->_getDI());
     $router->addResource('Robots', '/');
     $router->addResource('Products', '/products');
     $router->addResource('About', '/about');
     $router->handle('/products');
     $this->assertEquals(count($router->getRoutes()), 6);
     $router = new Phalcon\Mvc\Router\Annotations(false);
     $router->setDI($this->_getDI());
     $router->addResource('Robots', '/');
     $router->addResource('Products', '/products');
     $router->addResource('About', '/about');
     $router->handle('/about');
     $this->assertEquals(count($router->getRoutes()), 5);
     $router = new Phalcon\Mvc\Router\Annotations(false);
     $router->setDI($this->_getDI());
     $router->setDefaultNamespace('MyNamespace\\Controllers');
     $router->addResource('Namespaced', '/namespaced');
     $router->handle('/namespaced');
     $this->assertEquals(count($router->getRoutes()), 1);
     $router = new Phalcon\Mvc\Router\Annotations(false);
     $router->setDI($this->_getDI());
     $router->addResource('MyNamespace\\Controllers\\Namespaced', '/namespaced');
     $router->handle('/namespaced/');
     $router = new Phalcon\Mvc\Router\Annotations(false);
     $router->setDI($this->_getDI());
     $router->addResource('Robots');
     $router->addResource('Products');
     $router->addResource('About');
     $router->addResource('Main');
     $router->handle();
     $this->assertEquals(count($router->getRoutes()), 9);
     $route = $router->getRouteByName('save-robot');
     $this->assertTrue(is_object($route));
     $this->assertEquals(get_class($route), 'Phalcon\\Mvc\\Router\\Route');
     $route = $router->getRouteByName('save-product');
     $this->assertTrue(is_object($route));
     $this->assertEquals(get_class($route), 'Phalcon\\Mvc\\Router\\Route');
     $routes = array(array('uri' => '/products/save', 'method' => 'PUT', 'controller' => 'products', 'action' => 'save', 'params' => array()), array('uri' => '/products/save', 'method' => 'POST', 'controller' => 'products', 'action' => 'save', 'params' => array()), array('uri' => '/products/edit/100', 'method' => 'GET', 'controller' => 'products', 'action' => 'edit', 'params' => array('id' => '100')), array('uri' => '/products', 'method' => 'GET', 'controller' => 'products', 'action' => 'index', 'params' => array()), array('uri' => '/robots/edit/100', 'method' => 'GET', 'controller' => 'robots', 'action' => 'edit', 'params' => array('id' => '100')), array('uri' => '/robots', 'method' => 'GET', 'controller' => 'robots', 'action' => 'index', 'params' => array()), array('uri' => '/robots/save', 'method' => 'PUT', 'controller' => 'robots', 'action' => 'save', 'params' => array()), array('uri' => '/about/team', 'method' => 'GET', 'controller' => 'about', 'action' => 'team', 'params' => array()), array('uri' => '/about/team', 'method' => 'POST', 'controller' => 'about', 'action' => 'teampost', 'params' => array()), array('uri' => '/', 'method' => 'GET', 'controller' => 'main', 'action' => 'index', 'params' => array()));
     foreach ($routes as $route) {
         $_SERVER['REQUEST_METHOD'] = $route['method'];
         $router->handle($route['uri']);
         $this->assertEquals($router->getControllerName(), $route['controller']);
         $this->assertEquals($router->getActionName(), $route['action']);
         $this->assertEquals($router->getParams(), $route['params']);
         $this->assertEquals($router->isExactControllerName(), true);
     }
 }