我正在为 codeigniter 使用 Phil rest 库。根据他的文档,rest api 可以有我们想要的任意数量的参数,url 的结构如下:
/method/{resource/value}
翻译成这个
/users/id/1
我们可以附加更多属性,例如 /users/id/1/order/desc
但是,默认情况下,骨干网发送请求如下:
/users/1
使用的 HTTP 动词定义了我们正在执行的操作
所以,这个问题是 2 合 1。 问题 1 从客户端骨干模型的角度来看,我如何定义一个与 phil 的 Codeigniter 接口匹配的新 url 结构? 问题 2 其次,我想知道是否可以让 Phil 的 Codeigniter 库响应更简单的 url,就像来自骨干网的那些隐藏 id 并只传递值
instead of this -> /users/id/1 use this -> /users/1
编辑
对于问题 2,我发现可以使用Codeigniter 中的URI 段。但是有没有更好的方法来做到这一点?
<?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 动词完成其余的工作
非常感谢。