<?php
namespace Home\Controller;
use Think\Controller\RestController;
/**
* 操作名提交类型资源后缀 标准的Restful方法定义,例如 read_get_pdf
* 操作名_资源后缀 当前提交类型和defaultMethod属性相同的时候,例如read_pdf
* 操作名_提交类型 当前资源后缀和defaultType属性相同的时候,例如read_post
*
* 优先级总结:
* 在提交类型与默认一致,资源类型与默认一致的情况下
* 若方法只有动作名,则优先执行此方法
* 否则,优先执行全名方法(动作名_提交类型_资源类型)
*
* 在Rest操作方法中,可以使用$this->_type获取当前访问的资源类型,用$this->_method获取当前的请求类型。
*/
class TrestController extends RestController {
protected $defaultType = 'json';
protected $allowType = array('html','xml','json','rss','pdf');
//当提交类型与defaultMethod相同,会自动执行{动作_资源类型}
public function read_json(){
$this->response('read json','html');
}
//这里,提交类型与defaultMethod相同,在read动作请求json资源的情况下,会首先执行这个方法
public function read_get_json(){
$data = array(
'index' => 'key1',
'value' => array(
'id' => 1,
'name' => 'liu'
)
);
$this->response($data,'json');
}
//当资源类型与defaultType相同,会自动执行{动作_提交类型}
public function write_json(){
$this->response($_REQUEST,'json');
}
//这种情况同样存在优先级
public function write_get_json(){
$data = array(
'a' => I('a'),
'b' => I('b')
);
$this->response($_REQUEST,'json');
}
public function eat_get_xml(){
$this->response('im writting xml file');
}
public function eat_pdf(){
$this->response('im eatting pdf');
}
public function write_post_json(){
$this->response(array('0'=>'write','1'=>'post','2'=>'json'),'json');
}
}
如上述代码,我尝试了TP的REST,接下来,我要通过ajax向这一Controller发起请求,期间我会发送多个参数,但是最终返回结果是:无效的操作:write,不知道ajax跟TP的REST结合到底该怎样做了,请各位指教
QQ图片20150317174651.png
( 16.7 KB 下载:8 次 )
最佳答案
