Shahrukh
Shahrukh

Reputation: 478

Laravel Apply middlware on specific route of resource route controller

I want to apply middleware on create route of resource controller but confused how to set middleware. Normally we can add middleware like this

Route::get('api/users/{user}', function (App\Models\User $user) {
    return $user->email;
})->middleware('name');

but when we have a resource controller so how could I apply middleware on single route of resource controller.

Route::resource('front-pages','Admin\FrontPagesController');

Upvotes: 0

Views: 1686

Answers (3)

Yziaf_07
Yziaf_07

Reputation: 7

Route::middleware('name')->resource('front-pages' , 'Admin\FrontPagesController');

refer laravel documentation :https://laravel.com/docs/8.x/middleware#introduction

Upvotes: 0

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

create a __construct() function

in FrontPagesController.php

public function __construct()
{
    $this->middleware('auth', ['except' => ['index','show']]);
}

ref link https://laravel.com/docs/8.x/controllers#controller-middleware


all the possible functions

/**
 * Instantiate a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');

    $this->middleware('log')->only('index');

    $this->middleware('subscribed')->except('store');
}

Upvotes: 4

Muhammad Tariq
Muhammad Tariq

Reputation: 175

You can use

 Route::resource('front-pages','Admin\FrontPagesController')->only('fornt-page.whatever name');

And you can group it with middleware

Upvotes: -1

Related Questions