php – Codeigniter路由和REST服务器

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – Codeigniter路由和REST服务器全部内容,希望文章能够帮你解决php – Codeigniter路由和REST服务器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。