微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Codeigniter路由和REST服务器

我正在尝试为我的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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐