Hello everyone, I am stuck in the process of learning laravel policy. I know the difference between laravel policy and middleware, but I still don’t know how to apply the policy. Can the moderator provide a complete small program? Thank you very much.
This is an image answer about policy that I found on stackoverflow, http://stackoverflow.com/ques...
Then this is the code I stored on github when I was practicing. If anyone is willing to add the policy function to my code, I would be very grateful. You can use the policy for any function, as long as you add the policy to it
https://github.com/GoogleYY/s...
Policy
和Gate
结合起来使用就行,不复杂。Policy
的注册在AuthServiceProvider里,如注册一个AccountPolicy::class => Account::class
,就表示当前User
是否有权限对Account
这个Model
Add, delete, modify and check.In
AccountPolicy
针对增删改查操作写上授权逻辑,如针对Delete
操作写上$user->id === $account->user_id
authorization logic.How to trigger this authorization logic? Can be used
Model Event
触发,如在EventServiceProvider::boot()
riGate与Policy的关系类似于Route与Controller的关系。
After the email help from foreign stackoverflow master Amit Gupta, it is now solved.
Step 1: First register the policy, and establish the connection relationship between the Model and the policy in AuthServiceProvider.php under the Providers folder, as follows:
The second step is to create a new PermissionPolicy through the php artisan make:policy command, and write the relevant permission control into the two policies through functions, as shown below:
The last step is to use the related functions set by this policy in the controller, as follows: