我正在使用Phil rest库进行codeigniter。 根据他的文档,rest api可以有我们想要的参数,url的结构如下:
/method/{resource/value}
转换为此
/users/id/1
例如,我们可以添加更多属性 /用户/ ID / 1 /顺序/降序
但是,默认情况下,骨干发送请求如下:
/users/1
HTTP VERB USED定义了我们正在执行的操作
所以,这个问题是2合1。 问题1 我如何从客户端骨干模型的角度定义一个匹配来自phil的Codeigniter接口的新url结构? 问题2 其次,我想知道是否有可能让Phil的Codeigniter库响应更简单的URL,就像那些来自主干的隐藏id而只是传递值
instead of this -> /users/id/1 use this -> /users/1
修改
对于问题2,我发现可以使用Codeigniter中的URI segments。但是有更好的方法吗?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Example
*
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array.
*
* @package CodeIgniter
* @subpackage Rest Server
* @category Controller
* @author Phil Sturgeon
* @link http://philsturgeon.co.uk/code/
*/
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';
class Resource extends REST_Controller
{
function action_get(){
$data = array('method'=>'PUT','id'=>$this->uri->segment(3));
$this->response($data, 200); // 200 being the HTTP response code
}
function action_post(){
$data = array('method'=>'POST','id'=>$this->uri->segment(3));
$this->response($data, 200); // 200 being the HTTP response code
}
function action_delete(){
$data = array('method'=>'DELETE','id'=>$this->uri->segment(3));
$this->response($data, 200); // 200 being the HTTP response code
}
function action_put(){
$data = array('method'=>'DELETE','id'=>$this->uri->segment(3));
$this->response($data, 200); // 200 being the HTTP response code
}
}
要请求信息,我只需要执行 / Resource / method / {id} id是我们想要传递给控制器的值 HTTP动词执行其余的
非常感谢。