Codeigniter routing and REST server Codeigniter routing and REST server codeigniter codeigniter

Codeigniter routing and REST server


//www.mysite.com/players$route['players'] = 'players/index_get';//initial call to players index//www.mysite.com/players/rookies/** overrides the above **/$route['players/(:any)'] = 'players/index_get/$1';//Changing defaults index//www.mysite.com/players/rookies/10/4/** overrides the above **/$route['players/(:any)/(:num)/(:num)'] = 'players/index_get/$1/$2/$3';//Changing type,limit,offset//All routes that are similar, like above that follow the previous, override the preceding one. //www.mysite.com/players/create//overrides $route['players/(:any)']$route['players/create'] = 'players/index_post';class Players extends REST_Controller{    public $player_types = array();    public function __construct(){       $this->player_types = array(          'rookies', 'seniors'       );//manual assign or pull from db    }    /**     * Index     * $_GET    **/    public function index_get($type='rookies',$offset=0, $limit=0)//some defaults to show on initial call    {        // www.mysite.com/players/rookies        // $route['players/(:any)'] = 'players/index_get/$1';        // First uri segment, check to see if its a valid player 'type'        if(!in_array(strtolower($type), $this->player_types)){             //redirect ?             return;        }    }    /**     * Index     * $_POST    **/    public function index_post()    {        // Create a new player    }}


gregory, as you're stating yourself "/players refers to index method in the players controller", that means you shouldn't need to have $route['players'] = 'players/index' if your routing is clean.

You can have as many segments as you want and get URI class to distinguish them in your script. That means this URL "/players/rookies/limit/10/offset/5/key/abcdef" by default should lead to your players controller, rookies() method. And here's how you can get your segments:

function rookies () {    //$this->uri->segment (1); would return 'players' or 'limit'    //$this->uri->segment (8); would return 'abcdef' or false}

In addition for /players/limit to work:

function limit () {    $this->rookies();}

Edit 1

Here's another approach:

Routing rules:

$route['/players/rookies/limit/(:num)/offset/(:num)/key/(:any)'] = "players/get"$route['/players/limit/(:num)/offset/(:num)/key/(:any)'] = "players/get"

In controller

function get () {    //work with segments}