创建及规则
使用下面的命令生成一个资源控制器:
php think make:controller app\test\controller\Resource
效果图:
为这个资源控制器注册路由:
<?phpRoute::resource('blog','test/Blog');
设置后会自动注册7个路由规则,如下:
| 请求类型 | 生成路由规则 | 对应操作方法 |
|---|---|---|
| GET | rescorce |
index |
| GET | rescorce/create |
create |
| POST | resource |
save |
| GET | resource/:id |
read |
| GET | resource/:id/edit |
edit |
| PUT | resource/:id |
update |
| DELETE | resource/:id |
delete |
测试例子
我先注释其他定义的路由规则,以免产生冲突。
定义资源路由:
<?phpRoute::resource('resource', 'test/Resource');
先查看下命令生成的这个 resource类
<?phpnamespace app\test\controller;use think\Controller;use think\Request;class Resource extends Controller{/*** 显示资源列表** @return \think\Response*/public function index(){//}/*** 显示创建资源表单页.** @return \think\Response*/public function create(){//}/*** 保存新建的资源** @param \think\Request $request* @return \think\Response*/public function save(Request $request){//}/*** 显示指定的资源** @param int $id* @return \think\Response*/public function read($id){//}/*** 显示编辑资源表单页.** @param int $id* @return \think\Response*/public function edit($id){//}/*** 保存更新的资源** @param \think\Request $request* @param int $id* @return \think\Response*/public function update(Request $request, $id){//}/*** 删除指定资源** @param int $id* @return \think\Response*/public function delete($id){//}}
测试路由一GET 请求访问 resource
控制器:
<?php/*** 显示资源列表** @return \think\Response*/public function index(){//return 'Resouce/index';}
postman 测试:
结论:get 请求访问 resource 会执行 index()
测试路由一GET 请求访问 rescource/create
控制器:
<?php/*** 显示创建资源表单页.** @return \think\Response*/public function create(){//return 'Resouce/create';}
postman 测试效果:
结论:GET 请求访问 rescource/create 会执行 create()
