vincent15000's avatar

After upgrading to Laravel 12, the policies aren't registered anymore

Hello,

Laravel 12 autoregisters the policies.

I have upgraded an application from Laravel 9 to Laravel 12 and I have removed the AuthServiceProvider where the policies where declared.

But the policies aren't registered, I have tried to return true or false from the policy rule and it doesn't change anything.

@can('update', $category)
	<div>Always displayed, no matter if I return true or false from the update method in the policy
@endcan

Why do you suggest me to check ?

Thanks for your help.

V

0 likes
6 replies
Glukinho's avatar

Do you have policies placed in proper folders as described here?

https://laravel.com/docs/12.x/authorization#policy-discovery

Specifically, the policies must be in a Policies directory at or above the directory that contains your models. So, for example, the models may be placed in the app/Models directory while the policies may be placed in the app/Policies directory. In this situation, Laravel will check for policies in app/Models/Policies then app/Policies. In addition, the policy name must match the model name and have a Policy suffix. So, a User model would correspond to a UserPolicy policy class.

2 likes
vincent15000's avatar

Yes, the policies are placed in the right folder.

app\Policies

And the name of each policy matches the naming convention.

vincent15000's avatar

I just noticed that it wasn't a problem with the policies, but with @can and $this->authorize().

UPDATED => In fact the policy isn't called at all. I have added dd('ok'); at the beginning of a policy and nothing happens.

So the policies are not loaded.

vincent15000's avatar

Thank you for your suggestion, but I still get the same problem.

UPDATED => In fact the policy isn't called at all. I have added dd('ok'); at the beginning of a policy and nothing happens.

So the policies are not loaded.

I have tried to declare the policies inside the AppServiceProvider like recommended in the documentation, but it doesn't work better.

Gate::policy(Entreprise::class, EntreprisePolicy::class);
vincent15000's avatar

Effectively I had created a DefaultPolicy class with Gate::before() and I had extended all policies with this default policy.

Thank you very much ;).

Please or to participate in this conversation.