Spread the love

Resource controller is helpful when you want Create, Read, Update and Delete (CRUD) operations. If you have a model with same set of actions like crud then resource controller are useful. we can create resource controller easily using the artisan command.

php artisan make:controller ImageController --resource

Above command will generate controller ImageController in \app\Http\Controllers.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

As we can see it create some default methods for crud and we can register routes for this controller in routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;

Route::resource('images', ImageController::class);

Rout::resource will register all index, edit, show, create, store, update, delete routes.

Here is the list of routes

GET/imagesindeximages.index
GET/images/createcreateimages.create
POST/imagesstoreimages.store
GET/images/{photo}showimages.show
GET/images/{photo}/editeditimages.edit
PUT/PATCH/images/{photo}updateimages.update
DELETE/images/{photo}destroyimages.destroy

We can also define multiple resource in a single statement.

Route::resource(['images'=>ImageController::class,
                 'posts'=>PostController::class ]);

Also we can bind the middleware to it

Route::resource(['images'=>ImageController::class,
                 'posts'=>PostController::class ])->middleware('auth');

Create specific resource route

Sometime we oly want to create specific actions like create, store or index. So, Laravel also have this feature to include and exclude routes from resource using only and except methods

Route::resource(['images'=>ImageController::class])->only(['create']);
Route::resource(['images'=>ImageController::class])->only([ 'update', 'destroy']);

Api Resource controller

Above we created resource controller for web, now if we want to create the resource controller for REST api we can create in same way with artisan command

php artisan make:controller ImageController --api

In api we don’t want html action like create and edit. above command will create resource controller for api excluding method create and edit.

Also in the same way we can create route for api using apiResource method in api.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;

Route::apiResource('images', ImageController::class);
beenhere

Leave a Reply