Fuel Documentation

Routing

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 2 reserved routes. They are _root_ and _404_.

array(
    '_root_'  => 'welcome/index',
    '_404_'   => 'welcome/404',
),

Basic Routing

The route on the left is matched the request URI and if it is matched then the request is routed to the URI on the right.

This allows you to do things like the following:

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:

Here are some examples:

'routes' => array(
    'blog/(:any)'      => 'blog/entry/$1', // Routes /blog/entry_name to /blog/entry/entry_name
    '(:segment)/about' => 'site/about/$1', // Routes /en/about to /site/about/en
),

Advanced Routing

You can also have named parameters in your routes. This allows you to give your URI segments names, which can then be accessed from within your actions.

Example:

array(
    'blog/:year/:month/:id' => 'blog/entry', // Routes /blog/2010/11/entry_name to /blog/entry
),

In the above example it would catch the following /blog/2010/11/entry_name. It would then route that request to your 'entry' action in your 'blog' controller. There, the named params will be available like this:

$this->param('year');
$this->param('month');
$this->param('id');

HTTP verb based routing

You can route your URLs to controllers and actions based on the HTTP verb used to call them. This makes it quick and easy to make RESTful controllers.

Example:

array(
    // Routes GET /blog to /blog/all and POST /blog to /blog/create
    'blog' => array(array('GET', new Route('blog/all')), array('POST', new Route('blog/create'))),
),

You can use named parameters and regexes within your URL just like normal:

'routes' => array(
    'blog/(:any)' => array(array('GET', new Route('blog/show/$1'))),
),

Module Routing

When you use modules in routed mode, you can add custom routes to your module by including a routes.php in your modules config folder. These routes will be loaded when FuelPHP detects that the first URI segment refers to a module, and only the routes of that module will be loaded.

This implies that module routes can not be used to define routes for global routing or routes for other modules. In other words, the left side should always include the current module as first URI segment. For the right side you can use anything described in the sections above, it's perfectly fine to have a module route to an app controller or another module.