模块配置文件config.php中路由的开启及定义,需要注意如下几点:
1.restful路由配置是数组中嵌套数组,开发手册中已重点强调过,主要是为了区分其他路由配置;
2.method参数的值必须大写;
例:
'URL_ROUTER_ON' => true, // 是否开启URL路由
'URL_ROUTE_RULES' => array(
array('blog/:id', 'TestRestApi/test', '', array('method' => 'GET')),
array('blog/:id','TestRestApi/testput', '', array('method' => 'PUT')),
array('blog/:id','TestRestApi/testdelete', '', array('method' => 'DELETE')),
)
TestRestApiController的测试代码如下:
<?php
namespace Home\Controller;
use Think\Controller\RestController;
/**
* Restful测试控制器
*
* @author LincolnZhou
*/
class TestRestApiController extends RestController {
protected $allowMethod = array('delete');
public function rest() {
}
public function get() {
var_dump($this->_method);
}
public function test() {
echo 'test ok';
}
public function testput() {
var_dump(IS_PUT);
echo 'test put ok';
}
public function testdelete() {
var_dump(IS_DELETE);
echo 'test delete ok';
}
}
最佳答案