Google News
logo
FuelPHP - Interview Questions
What is Routing in FuelPHP?
Fuel's routing can range from simple static routes to advanced routes using HTTP verb based routing.
 
Routes are set in fuel/app/config/routes.php.
 
Reserved Routes : 

In Fuel there are 4 reserved routes. They are _root_, _403_, _404_ and _500_.
 
_root_ : The default route when no URI is specified.
_403_ : The route used when the application throws an HttpNoAccessException that isn't caught.
_404_ : The route used when the application throws an HttpNotFoundException that isn't caught.
_500_  : The route used when the application throws an HttpServerErrorException that isn't caught.
return array(
    '_root_'  => 'welcome/index',
    '_404_'   => 'welcome/404',
);
Basic Routing : The route on the left is compared to the request URI. If a match is found, the request is routed to the URI on the right.
 
This allows you to do things like the following:
return array(
    'about'   => 'site/about',
    'contact' => 'contact/form',
    'admin'   => 'admin/login',
);
Slightly Advanced Routing : You can include any regex into your routes. The left side is matched against the requests URI, and the right side is the replacement for the left, so you can use backreferences in the right side from the regex on the left. There are also a few special statements that allow you match anything or just a segment:
 
:any : This matches anything from that point on in the URI, does not match "nothing"
:everything : Like :any, but also matches "nothing"
:segment : This matches only 1 segment in the URI, but that segment can be anything
:num : This matches any numbers
:alpha : This matches any alpha characters, including UTF-8
:alnum : This matches any alphanumeric characters, including UTF-8

Here are some examples. Notice the subtle differences between :any and :everything :
return array(
    'blog/(:any)'       => 'blog/entry/$1', // Routes /blog/entry_name to /blog/entry/entry_name
                                            //   matches /blog/, does not match /blogging and /blog
    'blog(:any)'        => 'blog/entry$1',  // Routes /blog/entry_name to /blog/entry/entry_name
                                            //   matches /blog/ and /blogging, does not match /blog
    'blog(:everything)' => 'blog/entry$1',  // Routes /blog/entry_name to /blog/entry/entry_name
                                            //   matches /blog/, /blogging and /blog
    '(:segment)/about'  => 'site/about/$1', // Routes /en/about to /site/about/en
    '(\d{2})/about'     => 'site/about/$1', // Routes /12/about to /site/about/12
);
Advertisement