TuffRivers
6 months ago
554
2
Laravel

Can i add a laravel Policy to my Route?

Posted 6 months ago by TuffRivers

Currently i have a default policy that applies to a majority of my models, and i have the authorize within each controller action like sho

    public function index()
    {
 
        $this->authorize('index');

        return $this->sendResponse($this->card->getAllCards());

    }

Is there a cleaner way to add my $this->authorize at a higher level? Maybe in route middleware? Also i feel like grabbing the action dynamically from the route would drastically reduce the amount of code id have to write, i would just have to find a way to inject the resource to test the condition in my authorize code

for example if i have post /cards get /cards/1 get /cards

if route = get /resource
$action = 'index' 
$this->authorize($action, $resource)

if route get /resource/1
$action = 'show'
$this->authorize($action, $resource)

Thanks

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