Using Admin Middleware in some only Resource routing

Posted 3 months ago by ottaviane

Hi, I have this middleware:

class Admin
{  
     public function handle($request, Closure $next)
     {
       if(auth()->user()->isAdmin !== null)
       {
         if (auth()->user()->isAdmin == 1) {
             return $next($request);
         }
       }
       return redirect('/')->with('error', 'You have not admin access.');
     }
}

and I have this routes in web.php

Route::get('/raccolte/{id}/switch','[email protected]')->middleware('admin');
Route::get('/raccolte/{id}/{id2}/scambia','[email protected]')->middleware('admin');
Route::resource('raccolte','RaccolteController');

and with artisan route:list I have

 GET|HEAD  | raccolte                   | raccolte.index  | App\Http\Controllers\[email protected]   | web,auth       |
 POST      | raccolte                   | raccolte.store  | App\Http\Controllers\[email protected]   | web,auth       |
 GET|HEAD  | raccolte/create            | raccolte.create | App\Http\Controllers\[email protected]  | web,auth       |
 GET|HEAD  | raccolte/{id}/switch       |                 | App\Http\Controllers\[email protected]  | web,admin,auth |
 GET|HEAD  | raccolte/{id}/{id2}/scambia|                 | App\Http\Controllers\[email protected] | web,admin,auth |
 GET|HEAD  | raccolte/{raccolte}        | raccolte.show   | App\Http\Controllers\[email protected]    | web,auth       |
 PUT|PATCH | raccolte/{raccolte}        | raccolte.update | App\Http\Controllers\[email protected]  | web,auth       |
 DELETE    | raccolte/{raccolte}        | raccolte.destroy| App\Http\Controllers\[email protected] | web,auth       |
 GET|HEAD  | raccolte/{raccolte}/edit   | raccolte.edit   | App\Http\Controllers\[email protected]    | web,auth       |

I'd like to protect with middleware only a part of resource route:

|POST    |raccolte       |...store |[email protected] |web,auth|
|GET|HEAD|raccolte/create|...create|[email protected]|web,auth|

how can I do it in the best way so that 'php artisan route:list' shows me those filters? thanks, bye.

Please sign in or create an account to participate in this conversation.