Skip to main content

Posts

Showing posts with the label laravel policies

Guards, Gates and Policies code logic

  Guards Gates Gates are closure based means it can be access from throughout of our application. We define our Gates inside AuthserviceProvider under boot method. We can define many gates as we needed. we define gate for both roles and permissions. Now use Gate inside controller method, where you want to authorize user action. Policies In the policies, we map policy class to the specific model (Eluquant model). With the help of command we create policyClass like so. php artisan make:policy PostPolicy --model=Post  Define policy in the controller for a particular action. Define policy check  inside policy class view method. here we are checking is user_id is equal to the post user_id if check success  than it will return true and user can do this task. Authorization function in laravel But Laravel enables us to write policy to manage every policy check instead if writing checks in each method. For that, we will use laravel Authorize function. let make it make copy of...