Skip to content
Duy Nguyen edited this page May 28, 2015 · 1 revision

The router component allows defining routes that are mapped to controllers or handlers that should receive the request. A router simply parses a URI to determine this information.

Follow the Phalcon routes, all of routes declared in */conf/global.php like below:

$routes = [
    'app_routes' => [
        '/:controller' => [
            'module'     => 'common',
            'controller' => 1,
        ],
        '/:controller/:action/:params' => [
            'module'     => 'common',
            'controller' => 1,
            'action'     => 2,
            'params'     => 3,
        ],
        '/' => [
            'module'     => 'common',
            'controller' => 'index',
            'action' => 'index',
        ],
        '/admin' => [
            'module'     => 'admin',
            'controller' => 'index',
            'action' => 'index',
        ],
        '/admin/:controller' => [
            'module' 	 => 'admin',
            'controller' => 1,
        ],
        '/admin/:controller/:action/:params' => [
            'module'     => 'admin',
            'controller' => 1,
            'action'     => 2,
            'params'     => 3,
        ],
        '/admin/logout' => [
            'module'     => 'admin',
            'controller' => 'index',
            'action' 	 => 'logout'
        ]
    ]
];

When bootstrap the framework, it will be loader via initRouter() function located in /libs/Fly/Bootstrap.php.

$router = new PhRouter(false);
$router->setDefaultModule('common');

foreach ($config['app_routes'] as $route => $params) {
	$router->add($route, (array) $params)->setHostName($config->app_baseUri);
}
  • Default home page will be route to Common module, Index controller and Index action.

  • Path /admin will be route to Admin module, Index controller and Index action.

  • Mobile detect redirect declared in /libs/Fly/Bootstrap.php (lazy to move to config file :|) like below:

if (SUBDOMAIN == 'm') {
    $router->setDefaultModule('mobile');
    $mobileRoutes = [
        '/:controller' => [
            'module'     => 'mobile',
            'controller' => 1,
        ],
        '/:controller/:action/:params' => [
            'module'     => 'mobile',
            'controller' => 1,
            'action'     => 2,
            'params'     => 3,
        ],
        '/' => [
            'module'     => 'mobile',
            'controller' => 'index',
            'action' => 'index',
        ]
    ];

    foreach ($mobileRoutes as $route => $params) {
        $router->add($route, (array) $params)->setHostName('m.' . $config->app_baseUri);
    }
}
Clone this wiki locally