Skip to content
Advertisement

Possibility using gate authorization without logged user

Is there a possiblity to use Gate authorization without making use of user auth service?

For example I want to define a Gate:

JavaScript

When I do Gate::allows('morning') or in the controller $this->authorize('morning') is returning always false because there is no user logged in.

How can I make this work for just guest users?

Advertisement

Answer

Although there may be better solutions like using middleware or using abort helper function:

JavaScript

But there is solution for policies by declaring an “optional” type-hint or supplying a null default value for the user argument definition:

JavaScript

It may work for gates too but I haven’t test this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement