/** * @param Event $event * @param Application $application * @return mixed */ public function boot(Event $event, Application $application) { // Initializes router $router = new Router(false); $router->setDI($application->getDI()); // default routes if (isset($application->getConfig()->application->defaultRoutes)) { $defaultRoutesPath = $application->getConfig()->application->defaultRoutes; if (file_exists($defaultRoutesPath)) { require $defaultRoutesPath; } } // Modules routes (new Router\Loader())->autoload($application->getModules(), $router); $application->getDI()->setShared('router', $router); }
/** * Initializes routing */ public function initRoutes(Config $config) { //setups router $routerAdapter = new Standard($this->getDI()); $router = new Router($this->getDI(), $routerAdapter); //adds routes defined in modules $modules = $this->getApplication()->getModules(); foreach ($modules as $module) { $router->addModuleRoutes($module); } //adds routes defined in default file $defaultRoutesFile = $config->application->configDir . DIRECTORY_SEPARATOR . 'routes.php'; if (file_exists($defaultRoutesFile)) { $routes = (require $defaultRoutesFile); if (is_array($routes)) { $router->addRoutes($routes); } } //setup router rules $router->setup(); //registers router into DI $this->getDI()->set('router', $router->getRouter()); }
/** * @expectedException Exception */ public function testCheckRouteException() { $router = new Router(false); $router->setDI(\Phalcon\Di::getDefault()); $router->add('/test', ['controller' => 'Test', 'action' => 'index'], ['GET'], 2); }
public function testShouldMatchRouteWithEmptyHostName() { $_SERVER['HTTP_HOST'] = null; DI::getDefault()->get('config')->application->hostname = null; $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard(DI::getDefault()); $router = new \Vegas\Mvc\Router(DI::getDefault(), $routerAdapter); $router->addRoutes(['test' => ['route' => '/', 'paths' => ['module' => 'Mod', 'controller' => 'Con', 'action' => 'Act']]]); $router->setup(); $defaultRouter = $router->getRouter(); $route = $defaultRouter->getRouteByName('test'); $defaultRouter->handle('/'); $matchedRoute = $defaultRouter->getMatchedRoute(); $this->assertNotEmpty($matchedRoute); $this->assertEquals($route->getPaths()['module'], $matchedRoute->getPaths()['module']); $this->assertEquals($route->getPaths()['controller'], $matchedRoute->getPaths()['controller']); $this->assertEquals($route->getPaths()['action'], $matchedRoute->getPaths()['action']); }