FuelPHP - Routing


Advertisements


Routing maps request an URI to a specific controller's method. In this chapter, we will discuss the concept of routing in FuelPHP in detail.

Configuration

Routes configuration file is located at fuel/app/config/routes.php. The default routes.php file is defined as follows −

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

Here, _root_ is the predefined default route, which will be matched when the application is requested with root path, / e.g. http://localhost:8080/. The value of the _root_ is the controller and the action to be resolved when it is matched. welcome/index resolves to Controller_Welcome controller and action_index action method. Similarly, we have the following reserved routes.

  • root − The default route when no URI is specified.

  • 403 − It throws when HttpNoAccessException is found.

  • 404 − It returns when the page is not found.

  • 500 − It throws when HttpServerErrorException is found.

Simple Routing

The route is compared to the request URI. If a match is found, the request is routed to the URI. Simple routing is described as follows,

return array ( 
   'about'  => 'site/about', 
   'login' => 'employee/login', 
);

Here, about matches http://localhost:8080/about and resolves the controller, Controller_Site and action method, action_about

login matches http://localhost:8080/login and resolves the controller, Controller_Login and action method, action_login

Advanced Routing

You can include any regex into your routes. Fuel supports the following advanced routing features −

  • :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

For example, the following route matches URI http://localhost:8080/hello/FuelPHP and resolves controller, Controller_Welcome, and action action_hello

'hello(/:name)?' => array('welcome/hello', 'name' => 'hello'),

The corresponding action method in Controller_Welcome is as follows,

public function action_hello() { 
   $this->name = Request::active()->param('name', 'World'); 
   $message = "Hello, " . $this->name;  
   echo $message; 
}

Here, we have used Request class to get the name parameter from the URL. If the name is not found, then we are using World as default value. We will learn the Request class in the Request and Response chapter.

Result

Controller Welcome

HTTP Method Action

FuelPHP supports routes to match HTTP method prefixed actions. Following is the basic syntax.

class Controller_Employee extends Controller { 
   public function get_index() { 
      // called when the HTTP method is GET. 
   }  
   public function post_index(){ 
      // called when the HTTP method is POST. 
   } 
}

We can route your URLs to controllers and actions based on the HTTP verb in the configuration file as follows.

return array ( 
   // Routes GET /employee to /employee/all and POST /employee to /employee/create 
   ‘employee’ => array(array('GET', new Route(‘employee/all')), array('POST', 
      new Route(‘employee/create'))), 
);


Advertisements