How to add user access control in Laravel (ACL) [Updated 2019]

HOW TO ADD USER ACCESS CONTROL IN LARAVEL (ACL) [UPDATED 2019]

How to add user access control in Laravel (ACL) [Updated 2019]



Hi Coders, Today we are going to add an access control system in our laravel project. If you are building any laravel project then you can follow along and after making some changes things will be working for you. So let’s start





What access control will do

It Will secure your pages, limit user usability based on user role so admin will have maximum access and the user will have minimum access.
So there are many packages available for ACL in laravel. But the one we are going to use is “Laratrust”.You can check it here laratrust .Now let’s begin and follow the steps given below as we going to install this functionality from scratch.



Create a Laravel project if don’t have it already

So starting with the local server. You should have XAMPP installed on your computer. Xampp is a program that provides us server locally so that we can develop our apps using it.





Download and install Xampp from here
[ads]
Once installed you should get a control panel where you should have “Mysql and Apache” running. It should look like this



How to add user access control in Laravel (ACL) [Updated 2019]
[ads]
Now if all these services are running go and check url=” localhost/phpmyadmin ” you should see something like this





How to add user access control in Laravel (ACL) [Updated 2019]
[ads]
If you see this page that means you have localhost working now we need to proceed to laravel



Open command prompt and select directory “xampp/htdocs”. This should be





In windows C drive where windows are installed
In MacOS it should be in Applications under finder
In linux, it should be “opt/lampp”



Create laravel project by running below given command





     composer create-project --prefer-dist laravel/laravel lara

[ads]
You Should see the creation of a project in command prompt something like this. Here we are creating a “Lara” project.



How to add user access control in Laravel (ACL) [Updated 2019]
[ads]
if things are not working then you might want to install composer for that follow this link and install composer. Once installed re-create project using the above command.





Download and Install Composer From Here



Once installed run this command





      composer install

[ads]
How-to-add-user-access-control-in-Laravel-ACL-Updated-2019
[ads]
If things Worked as expected you should have “lara” project in your “xampp/htdocs” folder.



now we need to install nodejs on our computer.





Download it from here



and install it. In your command prompt/terminal run this command





      npm install

[ads]
How to add user access control in Laravel (ACL) [Updated 2019]
[ads]



Prepare to install Laratrust package

Before setting ACL we need to enable login functionality in our project so run below given command





     php artisan make:auth

[ads]
After refresh, you will see login and register link in your project. Its time to create migrate tables provided by laravel.
go to “.env ” file and update database details there.



How to add user access control in Laravel (ACL) [Updated 2019]
[ads]
then run





    php artisan migrate

[ads]



if you get error “fixed-laravel-migration-error-specified-key-was-too-long-max-key-length”
then follow this link to solve it





You should get “user, migration and password-reset table in database”;



Install Laratrust package

Link to the website is here





We need to run a command at this point to install laratrust.



      composer require "santigarcor/laratrust:4.0.*"

[ads]
Then In “config/app.php”.add this under “providers array”:





      Laratrust\LaratrustServiceProvider::class,

[ads]
After this In same “config/app.php”. Add below given line under “aliases”



     'Laratrust'   => Laratrust\LaratrustFacade::class,

[ads]
Now run another command in terminal





     php artisan vendor:publish --tag="laratrust"

[ads]
Now We need to run Laratrust migrations so we can create some database tables (laratrust will do this for us).



     php artisan laratrust:migration

[ads]
it will ask confirmation type “Yes” and hit the enter key.





It will add “laratrust_setup_tables.php” under our project “database/migrations”.To run this migration we run



     php artisan migrate 

[ads]
Now you will see new tables in the database as given below.





How to add user access control in Laravel (ACL) [Updated 2019]
[ads]
Now we need to create Models files



Run these commands





     php artisan make:model Role

     php artisan make:model Permission

[ads]
Under



Modify Your Models As Given Below – Include the code that you do not have in your model





app/Role.php



[ads]





      

[ads]



app/Permission.php





       

[ads]



app/User.php





     

[ads]
Now run the below-given command



     composer dump-autoload

[ads]
Now we have Laratrust installed and we are going to add user roles and permissions in our next post as this post is too long check here
[ads]
https://18.191.183.98/2019/06/25/how-to-use-laratrust-in-laravel-acl-updated-2019/
[ads]