Tự học Laravel: (25) Resource Controller

The implementation of Create, Read, Update, and Delete has been completed with regard to the article (Article). It’s a so-called CRUD. In fact, when implementing this CRUD, we have defined routes and controllers based on Laravel’s Resource Controler style. This time we will look at this Resource Controller.

In Laravel, standard CRUD is implemented in the following pattern.

When the operation target is an article (Article)

METHODHATEACTIONOPERATIONAL CONTENT
GET/articlesArticlesController@indexList display
GET/articles/createArticlesController@createCreate New
POST/articlesArticlesController@storeNew save
GET/articles/{id}ArticlesController@showExpress
GET/articles/{id}/editArticlesController@editEdit
PUT/PATCH/articles/{id}ArticlesController@updateUpdate
DELETE/articles/{id}ArticlesController@destroyDelete

RESORUCE CONTROLLERS

In Laravel, when creating a Controller, if you specify the -resource option, the method will be created by default as follows. This method is named based on the above pattern.

As an example, let’s create PostsController.php.

php artisan make:controller PostsController --resource
<?php
//app/Http/Controllers/PostsController.php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
 
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }
 
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
 
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
 
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
 
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

RESTFUL ROUTES

Define a route corresponding to PostsController.php.

//routes/web.php
 
Route::resource('posts', 'PostsController');

You can define routes of all patterns with only one line of Route :: resource. 
However, you can check the route with the following command, because it is not clear exactly what is defined if it is only this description.

php artisan route:list

+ -------- + ----------- + -------------------- + ------- ------------------------------------------------------ ----------- + -------------- +
| Domain | Method    | URI                | Name             | Action                                          | Middleware   |
+ -------- + ----------- + -------------------- + ------- ------------------------------------------------------ ----------- + -------------- +
|        | GET|HEAD  | posts              | posts.index      | App\Http\Controllers\PostsController@index      | web          |
|        | POST      | posts              | posts.store      | App\Http\Controllers\PostsController@store      | web          |
|        | GET|HEAD  | posts/create       | posts.create     | App\Http\Controllers\PostsController@create     | web          |
|        | DELETE    | posts/{post}       | posts.destroy    | App\Http\Controllers\PostsController@destroy    | web          |
|        | PUT|PATCH | posts/{post}       | posts.update     | App\Http\Controllers\PostsController@update     | web          |
|        | GET|HEAD  | posts/{post}       | posts.show       | App\Http\Controllers\PostsController@show       | web          |
|        | GET|HEAD  | posts/{post}/edit  | posts.edit       | App\Http\Controllers\PostsController@edit       | web          |
+ -------- + ----------- + -------------------- + ------- ------------------------------------------------------ ----------- + -------------- +

Note also where the Named Route is defined in the Name column.

You can also customize Route as follows when only some actions are required.

// routes/web.php
 
Route::resource('posts', 'PostsController')->only(['index', 'show']); 
Route::resource('posts', 'PostsController')->expect(['create', 'store', 'update', 'destroy']);
  • Since Route::resource(‘posts’, ‘PostsController’); was inserted in web.php only for confirmation, please delete it here.
  • We created PostController.php only for confirmation, so please delete it here.

MODIFY ROUTE IN ARTICLES

Since I learned Route::resource, I also correct Articles to this description.

// routes/web.php
 
//Route::get('articles', ['as' => 'articles.index', 'uses' => 'ArticlesController@index']);
//Route::get('articles/create', ['as' => 'articles.create', 'uses' => 'ArticlesController@create']);
//Route::get('articles/{id}', ['as' => 'articles.show', 'uses' => 'ArticlesController@show']);
//Route::post('articles', ['as' => 'articles.store', 'uses' => 'ArticlesController@store']);
//Route::get('articles/{id}/edit', ['as' => 'articles.edit', 'uses' => 'ArticlesController@edit']);
//Route::patch('articles/{id}', ['as' => 'articles.update', 'uses' => 'ArticlesController@update']);
//Route::delete('articles/{id}', ['as' => 'articles.destroy', 'uses' => 'ArticlesController@destroy']);
 
Route::resource('articles', 'ArticlesController');

Until now, the root of articles that has been defined over seven lines can actually be written in one line.

At first, confirm route:list with the artisan command.

php artisan route:list

+ -------- + ----------- + ------------------------- + - ---------------- + --------------------------------- ---------------- + -------------- +
| Domain | Method    | URI                     | Name             | Action                                          | Middleware   |
+ -------- + ----------- + ------------------------- + - ---------------- + --------------------------------- ---------------- + -------------- +
|        | GET|HEAD  | articles                | articles.index   | App\Http\Controllers\ArticlesController@index   | web          |
|        | POST      | articles                | articles.store   | App\Http\Controllers\ArticlesController@store   | web          |
|        | GET|HEAD  | articles/create         | articles.create  | App\Http\Controllers\ArticlesController@create  | web          |
|        | GET|HEAD  | articles/{article}      | articles.show    | App\Http\Controllers\ArticlesController@show    | web          |
|        | PUT|PATCH | articles/{article}      | articles.update  | App\Http\Controllers\ArticlesController@update  | web          |
|        | DELETE    | articles/{article}      | articles.destroy | App\Http\Controllers\ArticlesController@destroy | web          |
|        | GET|HEAD  | articles/{article}/edit | articles.edit    | App\Http\Controllers\ArticlesController@edit    | web          |
+ -------- + ----------- + ------------------------- + - ---------------- + --------------------------------- ---------------- + -------------- +

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.