In many cases, we need to perform different login operations in the foreground and background to limit user rights. Now we use laravel to achieve this requirement.
I. acquiring laravel
This is explained in the document and is relatively simple. You can use composer to download (I download slowly), and I will copy the empty items downloaded before.
II. Modify the configuration file
In this step, I only modify the database configuration in the. Env file.
III. operation project
At the project root, run
php artisan serve
Enter http://127.0.0.1:8000 in the browser to access your own project.
Get to the point
There are two ways to separate the front and back users, using two data tables or one data table.
I. use two tables (foreground user table and background user table)
1. Create data table
Run at project root
php artisan make:migration create_users_table
Executing the above command will generate the migration file XXXX ﹣ create ﹣ users ﹣ table.php in the database / migrations directory. This file is available when downloading the project and can be modified directly. Of course, it doesn’t matter to generate one more.
Modify XXXX ﹣ create ﹣ users ﹣ table.php file (if the above command has been executed, pay attention to the date of the XXXX part of the file when modifying, and do not make any mistakes).
Here I add an extra field to judge whether the user is allowed to log in.
Perform the migration after the modification is complete.
php artisan migrate
After successful execution, the corresponding table will be generated in the configured database
The users table is the foreground user table we want to use, and then generate a background user table in the same way
php artisan make:migration create_admins_table
Modify the contents of XXXXX ﹣ create ﹣ admins ﹣ table.php
Here, the two data tables are finished.
2. Basic login
You will see that there is more user.php in the app directory.
Open auth.php in the config folder
Then open the HTTP / controllers folder, which has an additional auth folder, which contains the controller we registered for login.
When refreshing the browser page, you will find that you have jumped to the login interface. Of course, no data has been added to the user table at present. Now add data to the user table:
php artisan make:seeder UsersTableSeeder
After running successfully, the userstableseeder.php file will be generated in the database / seeds directory. Open the file, and add the following content in the run method:
Then open databaseseeder.php and add:
After writing, run composer dump autoload
To view a data table:
The user you just created already exists. Log in and find that you can access it successfully.
3. Realize the slightly complex login of the foreground user
And in the attempt method:
(3) in the resources / Lang folder en folder, open auth.php and add:
'active' => 'This account is not allowed to login.'
(4) modify the blade template file:
Then log in again with the previous account password, and an error message will be prompted:
4. Realize background user login (separate front and back platform login)
And add index method to the controller
As you can see above, the middleware we use is auth, which will be the same as the foreground login, but now it is the background login.
php artisan make:middleware AuthAdmin
The modification is as follows:
php artisan make:middleware GuestAdmin
The modification is as follows:
(5) open the kernel.php file in HTTP directory, and add:
Now modify the previous route:
Now if you directly access 127.0.0.1:8000/admin, you will be prompted that the logincontroller does not exist. We can copy the logincontroller under the auth folder to the controllers directory, or create a new one.
Create a new view file login.blade.php, copy the login.blade.php under the auth folder to the current view file, remove the checkbox, modify the submission address to route (‘admin. Login ‘), and save.
2. One table implementation (the same table is distinguished by fields)
3. Modify route file
4. Modify the logincontroller.php file
Delete guard method and add credentials method
At present, it has basically implemented single table front and back login, but now there are bugs.
Displays error messages in the view layer.