ThinkPHP5 资源嵌套分组路由配置方法(可以根据需要实现多层嵌套,注意:资源嵌套分组路由配置顺序依次是嵌套层次递减) - 06

时间:2024-03-22 19:04:16

ThinkPHP5 资源嵌套分组路由配置方法(可以根据需要实现多层嵌套,注意:资源嵌套分组路由配置顺序依次是嵌套层次递减) - 06


use think\Route;
//资源嵌套分组路由配置方法(可以根据需要实现多层嵌套,注意:分组路由配置顺序依次是嵌套层次递减):
// 更改嵌套资源路由blogs资源的资源变量名为blog_id
//从表路由(分组路由配置顺序必须是从表路由优先配置,否则从表路由一直会路由到主表控制器的方法上)

Route::group(['name'=>'blogs/:blog_id/comments','prefix'=>'index/Comment/'], function() {
 Route::get('create$','create',['merge_extra_vars'=>true]);// create GET  http://contoso.org/blogs/128/comments/create?hi=hello&name=jason
 Route::post('/$','save',['merge_extra_vars'=>true]);      // save   POST http://contoso.org/blogs/128/comments?hi=hello&name=jason
 Route::get(':id/edit$','edit',['merge_extra_vars'=>true]);// edit   GET  http://contoso.org/blogs/128/comments/32/edit?hi=hello&name=jason
 Route::get(':id$','read',['merge_extra_vars'=>true]);     // read   GET  http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
 Route::put(':id$','update',['merge_extra_vars'=>true]);   // update PUT  http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
 Route::delete(':id$','delete',['merge_extra_vars'=>true]);// delete DELETE http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
 Route::get('/$','index',['merge_extra_vars'=>true]);      // index  GET  http://contoso.org/blogs/128/comments?hi=hello&name=jason
 //嵌套资源分组路由内部子资源分组路由千万别使用miss路由,否则将导致父资源分组路由无法执行,但是miss路由可以写在根资源路由里面,根控制器方法里面需要定义miss方法
 //必须注释掉 Route::miss('miss');如果发现路由URL地址与上述路由规则无法匹配,那么它会调用根资源里面定义的miss路由方法;
 }, [], ['blog_id' => '\d+','id' => '\d+']);

// create GET http://contoso.org/blogs/128/comments/create?hi=hello&name=jason
// save   POST http://contoso.org/blogs/128/comments?hi=hello&name=jason
// edit   GET http://contoso.org/blogs/128/comments/32/edit?hi=hello&name=jason
// read   GET http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
// update PUT http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
// delete DELETE http://contoso.org/blogs/128/comments/32?hi=hello&name=jason
// index  GET http://contoso.org/blogs/128/comments?hi=hello&name=jason

//主表路由(分组路由配置顺序必须是从表路由优先配置,否则从表路由一直会路由到主表控制器的方法上)
Route::group(['name'=>'blogs','prefix'=>'index/Blog/'], function() {
 Route::get('create$','create',['merge_extra_vars'=>true]);       // create GET  http://contoso.org/blogs/create?hi=hello&name=jason
 Route::post('/$','save',['merge_extra_vars'=>true]);             // save   POST http://contoso.org/blogs?hi=hello&name=jason
 Route::get(':blog_id/edit$','edit',['merge_extra_vars'=>true]);  // edit   GET  http://contoso.org/blogs/100/edit?hi=hello&name=jason
 Route::get(':blog_id$','read',['merge_extra_vars'=>true]);       // read   GET  http://contoso.org/blogs/100?hi=hello&name=jason
 Route::put(':blog_id$','update',['merge_extra_vars'=>true]);     // update PUT  http://contoso.org/blogs/100?hi=hello&name=jason
 Route::delete(':blog_id$','delete',['merge_extra_vars'=>true]);  // delete DELETE http://contoso.org/blogs/100?hi=hello&name=jason
 Route::get('/$','index',['merge_extra_vars'=>true]);             // index  GET  http://contoso.org/blogs?hi=hello&name=jason
 Route::miss('miss'); // 在根资源路由里面写miss路由
 }, [], ['blog_id' => '\d+']);

 
// create GET http://contoso.org/blogs/create?hi=hello&name=jason
// save   POST http://contoso.org/blogs?hi=hello&name=jason
// edit   GET http://contoso.org/blogs/100/edit?hi=hello&name=jason
// read   GET http://contoso.org/blogs/100?hi=hello&name=jason
// update PUT http://contoso.org/blogs/100?hi=hello&name=jason
// delete DELETE http://contoso.org/blogs/100?hi=hello&name=jason
// index  GET http://contoso.org/blogs?hi=hello&name=jason


// 主表路由生成的路由规则分别是:
// create GET  blogs/:blog_id/create
// save   POST blogs
// edit   GET  blogs/:blog_id/edit
// read   GET  blogs/:blog_id
// update PUT  blogs/:blog_id
// delete DELETE  blogs/:blog_id
// index  GET  blogs

// 从表路由生成的路由规则分别是:
// create GET  blogs/:blog_id/comments/create
// save   POST blogs/:blog_id/comments
// edit   GET  blogs/:blog_id/comments/:id/edit
// read   GET  blogs/:blog_id/comments/:id
// update PUT  blogs/:blog_id/comments/:id
// delete DELETE  blogs/:blog_id/comments/:id
// index  GET  blogs/:blog_id/comments


<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Blog extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'index';
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'create';
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'save';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function read($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'read'.' '.$blog_id;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function edit($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'edit'.' '.$blog_id;
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function update(Request $request, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'update'.' '.$blog_id;
    }

    /**
     * 删除指定资源
     *
     * @param  int  $blog_id
     * @return \think\Response
     */
    public function delete($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'delete'.' '.$blog_id;
    }
    
    public function miss()
    {
        echo 'Blog 路由方法不存在';
    }
}



<?php
namespace app\index\controller;
use think\Controller;
use think\Request;

class Comment extends Controller
{
    public function create($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is create, this is a Comment'.' blog_id='.$blog_id;
    }
    public function save(Request $request,$blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is save, this is a Comment'.' blog_id='.$blog_id;
    }
    public function read($id, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is read, this is a Comment'.' id ='.$id.' blog_id='.$blog_id;
    }

    public function edit($id, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is edit, this is a Comment'.' id ='.$id.' blog_id='.$blog_id;
    }
    
    public function update(Request $request, $id, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is update, this is a Comment'.' id ='.$id.' blog_id='.$blog_id;
    }
    public function delete($id, $blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is delete, this is a Comment'.' id ='.$id.' blog_id='.$blog_id;
    }
    public function index($blog_id)
    {
        echo $this->request->param('hi').'<br>';
        echo $this->request->param('name').'<br>';
        echo 'method is index, this is a Comment'.' blog_id='.$blog_id;
    }
}