Codeigniter routing and REST server

I am trying to get the following URLs for my API (I am using Codeigniter and Phil Sturgeon REST server library ):

/players -> refers to index method in the players controller /players/rookies -> refers to rookies method in the players controller 

I do not want the url to have a trailing "index"

 /players/index 

This is not a problem when I define the routes as follows:

 $route['players'] = 'players/index'; 

Everything works as expected.

My problem is that I need additional URL segments, for example:

 /players/rookies/limit/10/offset/5/key/abcdef 

The above example works, but the following:

 /players/limit/10/offset/5/key/abcdef 

I get the following error: {"status":false,"error":"Unknown method."} Obviously, there is no limit method in my controller.

How do I configure the route.php configuration file so that these URLs work correctly?

Any help is much appreciated!

+4
source share
2 answers
 //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 } } 
+3
source

gregory, since you declare yourself "/ players refer to the index method in the player controller", this means that you do not need to have $ route ['players'] = 'players / index' if your routing is clean.

You can have as many segments as you want and get a URI class to distinguish them from your script. This means that by default, this URL / player / rookies / limit / 10 / offset / 5 / key / abcdef should lead to your player controller, rookies (). And here is 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 } 

Also for / players / limit to work:

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

Change 1

Here's a different 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 the controller

 function get () { //work with segments } 
0
source

All Articles