Resources

Table des matières

1. Introduction

https://laravel.com/docs/5.6/controllers#resource-controllers

When working with a controller, we’ll most probably create a route for a GET action, one for a POST, one for a DELETE, etc. For instance:

// GET {URL}/todo/{id} -> show the todo #{id}
Route::get('todo/{id}', 'TodoController@show')->where('id', '[0-9]+');

// POST {URL}/todo -> the form is being submitted -> only for logged in users
Route::post('todo', ['uses' => 'TodoController@postForm'])->middleware('auth');

// GET {URL}/todo/{id}/edit -> edit an existing item in a form -> only for logged in users
Route::get('todo/{id}/edit', 'TodoController@edit')->where('id', '[0-9]+')->middleware('auth');

// DELETE {URL}/todo/{id} -> delete an existing item -> only for logged in users
Route::delete('todo/{id}', 'TodoController@delete')->where('id', '[0-9]+')->middleware('auth');

// PUT {URL}/todo -> update an existing item -> only for logged in users
Route::put('todo', 'TodoController@put')->middleware('auth');

One route for each typical CRUD actions so, our /routes/web.php file will contains a lot of routes for the same ... resource.

With the notion of resource, Laravel makes things easier by allowing to just define one single line of code : Route::resource('todo', 'TodoController');

2. Create a resource and use it

In a DOS prompt,

php artisan make:controller TodoController --resource

Laravel will automatically generate the file /app/Http/Controllers/TodoController.php with the prototype of each functions used for CRUD actions.

Edit /routes/web.php and replace any existing routes to Todo by

Route::resource('todos', 'TodoController');

To check the created routes, just run

php artisan route:list
Method URI Name Action Middleware
GET|HEAD todos todos.index App\Http\Controllers\TodoController@index web
POST todos todos.store App\Http\Controllers\TodoController@store web
GET|HEAD todos/create todos.create App\Http\Controllers\TodoController@create web
GET|HEAD todos/{todo} todos.show App\Http\Controllers\TodoController@show web
PUT|PATCH todos/{todo} todos.update App\Http\Controllers\TodoController@update web
DELETE todos/{todo} todos.destroy App\Http\Controllers\TodoController@destroy web
GET|HEAD todos/{todo}/edit todos.edit App\Http\Controllers\TodoController@edit web

Laravel will automatically associate vers, URI and actions and will create route’s name.

This means that, in our controller, we’ll be forced to use such actions: show and not display, destroy and not delete, ...

We see, Laravel will create routes for the 7 actions (index, create, show, update, ...). We can restrict the list:

Route::resource('todos', 'TodoController', ['except' => ['show', 'edit', 'update']]);

In this case, only 4 routes will be defined and show, edit and update will be rejected.

3. Add code to the controller

The generated /app/Http/Controllers/TodoController.php controller looks like:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TodoController 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)
    {
        //
    }
}

Now, code should be added for each function.