Based on the PhalconPHP router. Global router rules
are placed in /app/config/router.php
directory, but local module rules are in /app/modules/{Foo}/config/routes.php
where {Foo}
is a module name.
return array(
'home' => array( //Route name
'route' => '/', //Route url
'paths' => array(
'module' => 'Home',
'controller' => 'Frontend\Home',
'action' => 'index'
)
)
);
Available types:
return array(
'home' => array(
'route' => '/',
'paths' => array(
'module' => 'Home',
'controller' => 'Frontend\Home',
'action' => 'index'
),
'type' => 'default' //Optional
)
);
return array(
'login' => array(
'route' => '/login',
'paths' => array(
'module' => 'Auth',
'controller' => 'Frontend\Auth',
'action' => 'login'
),
'type' => 'static'
)
);
return array(
'products' => array(
'route' => '/products',
'paths' => array(
'module' => 'Product',
'controller' => 'Frontend\Product'
),
'type' => 'rest',
'params' => array(
'actions' => array(
'/' => array(
'index' => Vegas\Http\Method::GET,
'create' => Vegas\Http\Method::POST
),
'/{id}' => array(
'show' => Vegas\Http\Method::GET,
'update' => Vegas\Http\Method::PUT,
'delete' => Vegas\Http\Method::DELETE
)
)
)
)
);
return array(
'not-found' => array(
'route' => '',
'paths' => array(
'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 array(
'not-found' => array(
'route' => '/foo',
'paths' => array(
'module' => 'Foo',
'controller' => 'Frontend\Foo',
'action' => 'index'
),
'type' => 'default',
'params' => array(
'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());
}