# Example of Defining Routes

The list below are defined routes.

For auto routing, your controller should NOT have

    $autoroute=FALSE;

You need to enable auto route in common.conf.php as well

    $config['AUTOROUTE'] = TRUE;

Auto routes are accessed as http://domain.com/classname/methodname/param1/param2. If your class is named in a camel Case convention, you access it with a '-'. Parameters in auto route are accessed with integer Index instead of a key. Example:

    class HelloWorldController extends DooController{
        public function sayhi(){
            if(!empty($this->params)){
                print_r($this->params);
                echo 'Bye! '. $this->params[2];
            }
        }
    }
    # You access it via: http://domain.com/hello-world/sayhi/I am DooPHP/Second-name-here/Darkredz
    # You will get a result like this:
    Array
    (
        [0] => I am DooPHP
        [1] => Second-name-here
        [2] => Darkredz
    )
    Bye! Darkredz

Defining Fixed routes. The action method needs to be a public non-static function.

     // Define your URI routes here.
     // $route[Request Method][Uri] = array( class, action method, [options, etc.])

     $route['*']['/'] = array('MainController', 'index');
     $route['get']['/url'] = array('MainController', 'url');
     $route['post']['/news/update'] = array('NewsController', 'update');

A route with parameters.

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

Then you access the params in the Controller via:

    public function getNews(){
        echo $this->params['id'];
    }

    public function archive(){
        echo $this->params['year'];
        echo $this->params['month'];
    }

Here's how you define with extension name.

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

Parameters & extension name.

    $route['*']['/food/list/:id'] = array('RestController',
                                          'listFood',
                                          'extension'=>'.json'
                                         );
    $route['post']['/food/create/:id'] = array('RestController',
                                               'createFood',
                                               'extension'=>'.json');

Here's how you define with extension name.

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

Routes redirection:

    // here's how you do redirection to an existing route internally
    // http status code is optional, default 302 Moved Temporarily
    $route['*']['/about'] = $route['*']['/home'] = $route['*']['/'];
    $route['*']['/easy'] = array('redirect', '/simple');
    $route['*']['/easier'] = array('redirect', '/simple', 301);

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

If your Controller is in a sub folder. For example, it's in protected/controller/admin/AdminController.php

    $route['*']['/admin'] = array('admin/AdminController', 'index');

If you need Authentication for it quickly:

    //Http digest auth and subfolder example
    $route['*']['/admin'] = array('admin/AdminController', 'index',
                                  'authName'=>'Food Api Admin',
                                  'auth'=>array('admin'=>'1234', 'demo'=>'abc'),
                                  'authFailURL'=>'/admin/fail');

    //Instead of redirect when fail, just show a message, authFail
    $route['*']['/admin'] = array('admin/AdminController', 'index',
                                  'authName'=>'Food Api Admin',
                                  'auth'=>array('admin'=>'1234', 'demo'=>'abc'),
                                  'authFail'=>'Please login to the admin site!');

If you want to filter the parameters, you can use regular expressions.

    // This will only match value of Year 4 digits and Month 2 digits
    $route['*']['/news/:year/:month'] = array('NewsController', 'show_by_date',
                                                'match'=>array(
                                                            'year'=>'/^\d{4}$/',
                                                            'month'=>'/^\d{2}$/'
                                                         )
                                             );

Almost identical routes are supported if you ever need them.

    /**
     * almost identical routes examples, must assigned a matching pattern to the parameters
     * if no pattern is assigned, it will match the route defined first.
     */
    $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]+/')
                                       );

BACK TO TOP