DooPHP - fastest MVC based PHP framework

Learn More

Defining DooPHP URLs

The definitive guide to DooPHP

Discussions

Have a question? Post it at the forum.

Defining DooPHP URLs

DooPHP has a sophisticated URI router. You can make a website with clean and human-readable URLs with the framework. With clean and nicely formatted URL, search engine optimization can be done easily.

URL example:
http://example.net/index.php?section=news&year=2009&month=08&day=31
becomes
http://example.net/news/2009/08/31


Basic Example

In DooPHP, all your URL are defined in a single file named routes.conf.php. You can imagine that the php script is a Site map of your web application. A URL is defined and link to a method in a controller class.

$route['*']['/home'] = array('MainController', 'index');

The example above is a simple definition of a URL route.

When you access the URL http://domainname.com/home , the router will create an instance of MainController and the index method will be executed.

The routes are defined in a highly readable and easy to maintain way:

$route['*']['/']        = array('MainController', 'index');
$route['*']['/example'] = array('MainController', 'getExample');
$route['*']['/error']   = array('ErrorController', 'index');

All routes are define in protected/config/routes.conf.php. They are defined in this format:

$route[Request Method][Uri] = array(Controller, action method, other options)


RESTful routes

Routes defined in DooPHP are RESTful in native. You can make RESTful APIs with the help of Doo's route system too.

4 types of request methods are supported, GET, POST, PUT, DELETE. If you want a route to be accessed in any of the request methods, use *

// Can be access by any request methods
$route['*']['/api/list'] = array('RestController', 'listFood');

// Post request only
$route['post']['/api/create'] = array('RestController', 'createFood');

// Put request only
$route['put']['/api/update'] = array('RestController', 'updateFood');

// Delete request only
$route['delete']['/api/delete'] = array('RestController', 'deleteFood');  


Parameters in route

In a web application, you might want to utilize the use of parameters in routes.

$route['*']['/archive/:year/:month'] = array('NewsController', 'archive');

Users can passed in values to your app, eg. http://domain/archive/2009/12
In the NewsController you can get the values by calling $this->params:

class NewsController extends DooController{
    function archive(){
        echo $this->params['year'];
        echo $this->params['month'];
    }
}


Extension name

Some times it is great to add extension at the end of a URL (great for REST api). If you need to do so, simply add the extension name in your routes:

$route['*']['/simple.rss'] = array('FeedController', 'getRss');
$route['*']['/simple.atom'] = array('FeedController', 'getAtom');

It is a bit different if you want to have add it to a route with parameters:

$route['*']['/news/list/:id'] = array('FeedController',
                                      'listNews',
                                      'extension'=>'.json'
                                     );

//Or multiple extension names.
$route['*']['/news/list/:id'] = array('FeedController',
                                      'listNews',
                                      'extension'=>array('.json', '.xml')
                                     );

Users can access it via http://domain/news/list/168.json OR 168.xml



Redirections

Here's how you do redirection to an existing route internally

$route['*']['/simple'] = array('MainController', 'soSimple');

//Http status code is optional, default is 302 Moved Temporarily

$route['*']['/about'] = $route['*']['/home'] = $route['*']['/'];
$route['*']['/easy'] = array('redirect', '/simple');
$route['*']['/easier'] = array('redirect', '/simple', 301);

When you access about or home, you will be redirected to the main page. When you access easy or easier, you will be redirected to simple.

To redirect a local URL to another website just pass in the full URL (http://)

# External redirect
$route['*']['/doophp'] = array('redirect', 'http://doophp.com/');


Matching parameters

If you wanted to filter the parameter values in the route(instead of doing it in Controller), you can use Regular expressions to do some matching.

Below is an identical routes matching examples. You must assigned a matching pattern against parameter values. If no pattern is assigned, it will match the first route defined.

$route['*']['/news/:id'] = array('NewsController', 'show_news_by_id',
                                 'match'=> array(
                                            'id'=>'/^\d+$/'
                                         )
                                );

$route['*']['/news/:title'] = array('NewsController', 'show_news_by_title',
                                    'match'=> array(
                                                'title'=>'/[a-z0-9]+/'
                                            )
                                   );


Auto Routes

Auto routing is a real time saver. To use auto routing you would just need to enable it in common.conf.php.

$config['AUTOROUTE'] = true;

Let's say you have a Controller class like this:

class BlogController extends DooController{
     public function all(){
         //Display all blog posts
     }
}

It can be accesed it at http://localhost/blog/all

If you have your controller name in a camelCase style:

class CamelCaseController extends DooController{
     public function walking(){
         //Display all photos on camel walking
     }
}

It can be accesed it at http://localhost/camel-case/walking



Do visit and test out the URI routing demo in the demos section. It serves as a great example on how to fully utilize the routing system in DooPHP