Routing

As with other frameworks, FuelPHP has fairly extensive routing capabilities. In this section, we will run through the basics.

Firstly, there are a couple of reserved routes; they are: _root_ and _404_. The _root_ key is used when there is no URL specified; for example, the home page or root page. The second (_404_) is for when the requested content controller or view can't be found.

The routes exist in the config folder of the application in a file called routes.php. Let's load the routes.php file from the following path comprising the following code:

[rootOfProject]/fuel/app/config/routes.php

<?php
return array(
    '_root_'  => 'welcome/index',  // The default route
    '_404_'   => 'welcome/404',    // The main 404 route
);

As you can see from the routes configuration file, the routes are stored as an array. The key on the left is matched to the URL and then the items on the right are executed by FuelPHP. This is fairly straightforward, but can lend itself to complex URL and keyword matching.

The simplest routes are the ones that match a URL string directly to a controller and an action. These can look as follows:

return array(
    'about'  => 'welcome/about',  // The action method in the welcome controller
    'contact'   => 'about/contact',    // The contact method in the about controller
);

Our applications would tend to be fairly dynamic in nature, so specifying all the possible routes in the application would be a tedious job. This is where more advanced routing comes in handy. For this, we can use keywords and basic regular expressions to match strings in the URL and translate them to the controller method. These expressions make use of keywords preceded by a colon as follows:

  • :any: This keyword matches anything from that point on.
  • :segment: This keyword matches a single segment in the URL. The segment can be anything. This can be useful for language strings in the URL.
  • :num: This keyword matches numeric values in the URL.
  • :alpha: This keyword matches any alpha characters.
  • :alnum: This keyword matches any alphanumeric characters.

Using the following code, the router will match any journal entry and then send the entry name to the entry method in the journal controller:

'journal/(:any)' => 'journal/entry/$1',

The code in the following example will allow any preceding segment to be used for URLs like /en/contact and will send the language flag as a variable to the contact method in the site controller. The final URL would be something like /site/contact/en:

'(:segment)/contact' => 'site/contact/$1',

As developers, we often try to make our code more readable by choosing clear and descriptive variable names. The same can be done with more advanced routing in FuelPHP, as it allows you to use named parameters in the routes. These named segments can then be accessed from within your methods or actions. For example:

return array(
    'journal/:year/:month/:day/:id' => 'journal/entry', 
);

In this route, /journal/2013/11/5/name would be routed to the entry method within the journal controller. In the entry method, we can get the named segments in the following manner:

$this->param('year'),
$this->param('month'),
$this->param('day'),
$this->param('id'),

FuelPHP uses regex for the named segments to work within the route. Each segment counts as a back reference, for example, the $1 and $2 regex placeholders—we often use these when making use of regular expressions in PHP. Back references are a regex term and more information can be found at the following website:

http://www.regular-expressions.info/brackets.html#usebackrefinregex

In a route of :name/(d{2}, the digit (d{2})would be found using the variable $2 and the variable $1 would return the value of the :name segment.

We mentioned the RESTful controller template in the previous chapters. These can be used in conjunction with verb-based routing to direct requests to the correct methods in the RESTful controllers. This allows the route to a certain URL to be routed through to different methods and controllers in order to fit with the functionality.

For example:

  • A POST request to /journal could be routed to the create method in the journal controller
  • A GET request to /journal could be routed to the index method in the journal controller

These requests all follow the recommended use of the HTTP verbs to perform actions in the application and the route in the routes.php file would look something like the following:

return array(
    'journal' => array( 
        array('GET', new Route('journal/index')),
        array('POST', new Route('journal/create')),
    )
);

We could do something similar for the PUT and DELETE verbs and use regex and named parameters to make it easier to get the relevant information from the URL.

If you are working with user profile information and data, we would look to use HTTPS or secure connections; again, routing in FuelPHP supports this. The following example would only load the route if the request is sent via HTTPS, rather than just HTTP:

return array(
    'user/(:any)' => array( 
            array( 'GET', new Route( 'user/view/$1' ), true ) 
        ),
); 

The third parameter ensures that the named route is only used when HTTPS is used.

During development, we often rearrange the structure of the application to reflect the changing functionality. One feature of routes in FuelPHP, that aim to make routing easier, is called named routes and reverse routing. With this feature, instead of editing all of our views, we can simply change the named route in the main routes.php file. For this to work, we need to use the name of the route in our views. In the following example, we change 'admin/app/dashboard' to 'admin/dashboard':

return array(
    'admin/app/dashboard' => array('admin/dashboard', 'name' => 'admin_dashboard'),
);

In our views that need to link to the dashboard, we would use the following anchor code:

echo Html::anchor(Router::get('admin_dashboard'), 'Dashboard'),

Note

This only works for application code and will not work for module routes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.246.218