You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. Laravel Policies are intended to define rules between two models. However, in more complex applications multiple rules must be checked after receiving request. For example, when updating a Post:
Such rules can be extracted to a trait as protected methods and then used in Policies by public methods cross-domain, or Gates can be defined in ServiceProvider. This way, $user->can() and @can work.
However, I would like to define rules as PHP classes and then use Laravel Actions to connect it to Laravel so I can use $user->can() and @can and other framework helpers.
Would it be feasible to create AsGate trait in Laravel Actions? AFAIK there is no Gate class in Laravel, however Gate rules are defined in functions so maybe they could be auto-registered as Gates? String name could be implicit from MyGateAction::class and then it could be used as Gate::allows(MyGateAction::class). In Laravel docs there is this example:
if (! Gate::allows('update-post', $post)) {
abort(403);
}
This could be used like this:
if (! Gate::allows(MyGateAction::class, $post)) {
abort(403);
}
Would it be feasible to create AsPolicy trait in Laravel Actions? This could be more complicated, however it is similar to Controllers where the handle/__invoke method is implicitly used, so MyPolicyAction could require just one method handle(). In Laravel docs there is this example:
if ($request->user()->cannot('update', $post)) {
abort(403);
}
This could be used like this, which is not as pretty as the Gate methodless alternative, however it is powerful:
if ($request->user()->cannot('handle', MyPolicyAction::class, [$post])) { // the $post is used just like additional parameter
abort(403);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi. Laravel Policies are intended to define rules between two models. However, in more complex applications multiple rules must be checked after receiving request. For example, when updating a Post:
Such rules can be extracted to a trait as protected methods and then used in Policies by public methods cross-domain, or Gates can be defined in ServiceProvider. This way,
$user->can()
and@can
work.However, I would like to define rules as PHP classes and then use Laravel Actions to connect it to Laravel so I can use
$user->can()
and@can
and other framework helpers.Would it be feasible to create AsGate trait in Laravel Actions? AFAIK there is no Gate class in Laravel, however Gate rules are defined in functions so maybe they could be auto-registered as Gates? String name could be implicit from MyGateAction::class and then it could be used as Gate::allows(MyGateAction::class). In Laravel docs there is this example:
This could be used like this:
Would it be feasible to create AsPolicy trait in Laravel Actions? This could be more complicated, however it is similar to Controllers where the
handle
/__invoke
method is implicitly used, so MyPolicyAction could require just one methodhandle()
. In Laravel docs there is this example:This could be used like this, which is not as pretty as the Gate methodless alternative, however it is powerful:
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions