How to set routes in FuelPHP?

Tired on accessing the site with lengthy FuelPHP URL's? Then try this. 

Note : This article assumes that the user is familiar with FuelPHP basics.

For configuring routes you need to follow the steps below -

1) Goto your FuelPHP root folder -> fuel -> app -> config and then open the file named routes.php
2)

You will notice that there is somthing already specified in the file as follows.

    '_root_'  => 'welcome/index',  // The default route
    '_404_'   => 'welcome/404',    // The main 404 route

These are the predefined routes set by FuelPHP. You only need to add yours. You need to first understand the both sides of the array i.e. left side (key) and right side (value).

The key indicates to which controller will be called. The Key refers to what you put in the URL and the Value refer to what your controller URL is. Suppose you have a controller URL like 'welcome/index/all/food' where index is your function and all and food are the URL segments.

You can shorten this as follows with the help of routes as follows-

'welcome/(:any)/(:any)' => 'welcome/index/$1/$2',

So now your URL will be 'welcome/all/food'. Similarly in this way your can shorten may URLs and reduce lengthy URLs. Static URLs may also be specified.

You can find more exampes at the FuelPHP Documentation site.

FuelPHP Routing

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.