php – Codeigniter路由和REST服务器
发布时间:2020-12-13 16:49:42 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试为我的API实现以下URL(我正在使用Codeigniter和Phil Sturgeon的 REST server library): /players - refers to index method in the players controller/players/rookies - refers to rookies method in the players controller 我不希望URL有一个
我正在尝试为我的API实现以下URL(我正在使用Codeigniter和Phil Sturgeon的
REST server library):
/players -> refers to index method in the players controller /players/rookies -> refers to rookies method in the players controller 我不希望URL有一个尾随的“索引” /players/index 当我像这样定义路由时,这完全没问题: $route['players'] = 'players/index'; 一切都按预期工作. 我的问题是我需要额外的网址段,如下所示: /players/rookies/limit/10/offset/5/key/abcdef 上面的示例有效,但以下内容不起作用: /players/limit/10/offset/5/key/abcdef 我收到以下错误:{“status”:false,“error”:“Unknown method.”} 如何设置routes.php配置文件以使这些URL正常工作? 任何帮助深表感谢! 解决方法//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 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |