1. 程式人生 > >ThinkPHP5 資源巢狀分組路由配置方法(可以根據需要實現多層巢狀,注意:資源巢狀分組路由配置順序依次是巢狀層次遞減)

ThinkPHP5 資源巢狀分組路由配置方法(可以根據需要實現多層巢狀,注意:資源巢狀分組路由配置順序依次是巢狀層次遞減)


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;
    }
}