Based on the PhalconPHP router. Global router rules
are placed in /app/config/routes.php
directory, but local module rules are in /app/modules/{Foo}/config/routes.php
where {Foo}
is a module name.
return [
'home' => [ //Route name
'route' => '/', //Route url
'paths' => [
'module' => 'Home',
'controller' => 'Frontend\Home',
'action' => 'index'
]
]
];
Available types:
return [
'home' => [
'route' => '/',
'paths' => [
'module' => 'Home',
'controller' => 'Frontend\Home',
'action' => 'index'
],
'type' => 'default' //Optional
]
];
return [
'login' => [
'route' => '/login',
'paths' => [
'module' => 'Auth',
'controller' => 'Frontend\Auth',
'action' => 'login'
],
'type' => 'static'
]
];
return [
'page' => [
'route' => '/{slug}',
'paths' => [
'module' => 'Page',
'controller' => 'Frontend\Page',
'action' => 'index'
],
'type' => 'base'
]
];
return [
'products' => [
'route' => '/products',
'paths' => [
'module' => 'Product',
'controller' => 'Frontend\Product'
],
'type' => 'rest',
'params' => [
'actions' => [
'/' => [
'index' => Vegas\Http\Method::GET,
'create' => Vegas\Http\Method::POST
],
'/{id}' => [
'show' => Vegas\Http\Method::GET,
'update' => Vegas\Http\Method::PUT,
'delete' => Vegas\Http\Method::DELETE
]
]
]
]
];
return [
'not-found' => [
'route' => '',
'paths' => [
'module' => 'Home',
'controller' => 'Frontend\Error',
'action' => 'error404'
],
'type' => 'notfound'
]
];
Adding the parameter hostname we can constrain matching the route only when $_SERVER['HTTP_HOST']
is equals to defined hostname. Remember to define the base hostname in application config, for example:
in application config:
...
'hostname' => 'example.com'
...
return [
'not-found' => [
'route' => '/foo',
'paths' => [
'module' => 'Foo',
'controller' => 'Frontend\Foo',
'action' => 'index'
],
'type' => 'default',
'params' => [
'hostname' => 'foo.example.com'
]
]
];
The standard router can be replaced with your own custom router.
You can do it by overriding method initRoutes inside application bootstrap
Base routes initialization looks like:
protected function initRoutes()
{
//setups router
$routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard($this->di);
$router = new \Vegas\Mvc\Router($this->di, $routerAdapter);
//adds routes defined in modules
$modules = $this->application->getModules();
foreach ($modules as $module) {
$router->addModuleRoutes($module);
}
//adds routes defined in default file
$defaultRoutesFile = $this->config->application->configDir . DIRECTORY_SEPARATOR . 'routes.php';
if (file_exists($defaultRoutesFile)) {
$router->addRoutes(require $defaultRoutesFile);
}
//setup router rules
$router->setup();
$this->di->set('router', $router->getRouter());
}