首先,感谢流年,感谢Rest原创者Roy Fielding,感谢TVB,感谢CCTV,感谢我的父母、兄弟姐妹和乡亲们,以下省去10万毫无意义的致谢词……
接下来让我们来体验一下TPRestfull,不过再此之前,还不知道什么叫Rest的童鞋们,可以点击这个链接去了解一下。
http://zh.wikipedia.org/wiki/REST
已经了解过了吗?好吧,让我们开始吧!
一、TPRestfull的原理
根据不同的请求方式(REQUEST_MODE)和资源标识(resource identifier)把请求转发到相应的操作(action)。
二、使用TPRestful
要使用Rest模式,需要在项目的入口文件中添加模式定义:
define(‘THINK_MODE’,'Rest’); // 采用Rest部署
三、例子
<?php
class IndexAction extends Action
{
// request: curl http://localhost/index.php/Index/user
// request: curl http://localhost/index.php/Index/user -XGET
// retval: Index::user_get
public function user_get(){
echo __METHOD__;
$this->display();
}
// request: curl http://localhost/index.php/Index/user -XPOST
// retval: Index::user_post
public function user_post(){
echo __METHOD__;
}
// request: curl http://localhost/index.php/Index/user -XPUT
// retval: Index::user_put
public function user_put(){
echo __METHOD__;
}
// request: curl http://localhost/index.php/Index/user -XDELETE
// retval: Index::user_delete
public function user_delete(){
echo __METHOD__;
}
// request: curl http://localhost/index.php/Index/product.xml
// retval: Index::product_xml
public function product_xml(){
echo __METHOD__;
}
// request: curl http://localhost/index.php/Index/product.json
// retval: Index::product_json
public function product_json(){
echo __METHOD__;
}
}
?>
四、几点困惑
1、当存在Index::user()后,Restful就失效了,通过Rest方式统一访问到Index::user(),既然Restful的特色是可以根据请求类型和资源类型访问不同操作,那么是不是应该优化Restful访问呢?
2、通过URL可以直接访问到Index::user_post等操作?
3、通过浏览器访问 http://localhost/index.php/Index/user 获取到的模板文件名为user.html,而访问 http://localhost/index.php/Index/user_get得到的模板名却为user_get.html?通过CURL通过get、post等不通请求方式获取的都是同一个模板user.html?
4、TPRestfull优先请求方式再到资源方式?访问/Index/user.xml和/Index/user.json的结果都是Index::user_get。
五、感谢
非常感谢您能坚持看到这里,谢谢!欢迎来喷!