Codeigniter路由和REST服务器

编程入门 行业动态 更新时间:2024-10-20 09:26:26
本文介绍了Codeigniter路由和REST服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为我的API实现以下URL(我正在使用Codeigniter和Phil Sturgeon的 REST服务器库):

I'm trying to achieve the following URLs for my API (I'm using Codeigniter and Phil Sturgeon's REST server library):

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

中的菜鸟方法,我不希望URL的末尾带有索引

I don't want the URL to have a trailing "index"

/players/index

当我这样定义路线时,这根本没有问题:

This is no problem at all when I define the routes like so:

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

一切正常。

我的问题是我需要像这样的其他URL段:

My problem is that I need additional URL segments like so:

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

以上示例有效,但以下示例无效:

The above example works, but the following does not:

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

我遇到以下错误: { status:false, error:未知的方法。} 显然,我的控制器中没有 limit 方法。

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

如何设置我的route.php配置文件才能使这些URL正常工作?

How do I have to setup my routes.php config file to get these URLs to work properly?

任何帮助深表感谢!

推荐答案

//www.mysite/players $route['players'] = 'players/index_get';//initial call to players index //www.mysite/players/rookies /** overrides the above **/ $route['players/(:any)'] = 'players/index_get/$1';//Changing defaults index //www.mysite/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/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/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 } }

更多推荐

Codeigniter路由和REST服务器

本文发布于:2023-10-12 17:04:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485333.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   服务器   Codeigniter   REST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!