2

I want to apply authorization check on all my routes for a resource. For example:

  • Admin can create Doctor
  • Admin can update Doctor
  • Admin can view Doctor(s)
  • Admin can delete Doctor

Can I achieve this by applying "can" middleware on resource routes, something like:

Route::resource('doctors', 'DoctorsController')->middleware('can:*,App\Doctor');

Thanks!

TalESid
  • 2,304
  • 1
  • 20
  • 41

2 Answers2

0

In DoctorsController constructor you can use:

$this->middleware('can:<something>,App\Doctor');

Reference: https://stackoverflow.com/questions/28729228/laravel-5-route-resources-middleware

divyesh makvana
  • 384
  • 1
  • 5
  • 1
    I've already checked the linked question but actually here I don't want specific permission but I need **can** middleware on all permissions for the resource. So can I use it like `->middleware('can:*, App\Doctor')` with or without controller??? – TalESid Jul 19 '18 at 10:21
-1
Route::resource('doctors', 'DoctorsController', ['middleware' => ['middleware1', 'middleware2', 'middleware3']]);
ako
  • 2,000
  • 2
  • 28
  • 34
  • does this mean I have to write each route separately with can like `'DoctorsController', ['middleware' => ['can:create', 'can:update', ... ]]);` **???** – TalESid Jul 19 '18 at 10:32
  • You can create a custom middleware for example named `ManageDoctors` and inside that middleware check for all permissions `create, update, delete...` and then put `MangeDoctors` middleware on the resource route. – ako Jul 19 '18 at 10:35
  • please try to add more info to the answer to be clear about the solution – xploshioOn Jul 19 '18 at 16:52