Resource routing (CURD)



Resource routing

Resource routing can quickly generate a batch of routes, especially suitable for CURD operations

Supports setting resource routing for RESTFul requests as follows:

Route::resource('blog', 'Blog');

means that a resource named blog is routed to the Blog controller. The system will automatically register 7 routing rules, as follows:


##deleteDELETEdelete
IdentificationRequest typeGenerate routing rulesCorresponding operation method (default)
indexGETblogindex
createGETblog/createcreate
savePOSTblog#save
readGETblog/:idread
editGET blog/:id/editedit
updatePUTblog/:idupdate
blog/:id


##The specific controller pointed to is determined by the routing address. You only need to create the above corresponding operation method for the Blog controller. The following URL access can be supported:

http://serverName/blog/
http://serverName/blog/128
http://serverName/blog/28/edit

The corresponding methods in the Blog controller are as follows:

<?php
namespace app\controller;

class Blog
{
    public function index()
    {
    }

    public function read($id)
    {
    }

    public function edit($id)
    {
    }
}

You can quickly create a resource controller class through the command line (refer to the resources in the controller chapter later) Controller section).

You can change the default id parameter name, for example:

Route::resource('blog', 'Blog')
    ->vars(['blog' => 'blog_id']);

The method definition of the controller needs to be adjusted as follows:

<?php
namespace app\controller;

class Blog
{
    public function index()
    {
    }

    public function read($blog_id)
    {
    }

    public function edit($blog_id)
    {
    }
}

It can also be limited to execution when defining resource routing Method (identity), for example:

// 只允许index read edit update 四个操作
Route::resource('blog', 'Blog')
    ->only(['index', 'read', 'edit', 'update']);
    
// 排除index和delete操作
Route::resource('blog', 'Blog')
    ->except(['index', 'delete']);

The identification of resource routing cannot be changed, but the generated routing rules and corresponding operation methods can be modified.

If you need to change the corresponding operation of a resource routing identifier, you can use the following method:

Route::rest('create',['GET', '/add','add']);

After setting, the URL access becomes:

http://serverName/blog/create
变成
http://serverName/blog/add

Create the corresponding operation of the blog page The operation method has also changed to add.

Supports batch changes, as follows:

Route::rest([
    'save'   => ['POST', '', 'store'],
    'update' => ['PUT', '/:id', 'save'],
    'delete' => ['DELETE', '/:id', 'destory'],
]);

Resource nesting


Supports nesting of resource routes, for example:

Route::resource('blog', 'Blog');
Route::resource('blog.comment','Comment');

You can access the following address:

http://serverName/blog/128/comment/32
http://serverName/blog/128/comment/32/edit

The generated routing rules are:

blog/:blog_id/comment/:id
blog/:blog_id/comment/:id/edit

The corresponding operation method of the Comment controller is as follows:

<?php

namespace app\controller;

class Comment
{
    public function edit($id, $blog_id)
    {
    }
}

edit method The order of parameters can be arbitrary, but the parameter names must meet the definition requirements.

If you need to change the variable name, you can use:

// 更改嵌套资源路由的blog资源的资源变量名为blogId
Route::resource('blog.comment', 'index/comment')
    ->vars(['blog' => 'blogId']);

The operation method corresponding to the Comment controller is changed to:

<?php
namespace app\controller;

class Comment
{
    public function edit($id, $blogId)
    {
    }
}