Create Laravel Login And Registration Tutorial With Example.
Today, we will create a full login and register example in laravel.
It will include functionalities like registration, login, logout, remember me and forgot password.
It is very simple to implement authentication process in laravel.
We just need to trigger one command and make database connection.
All files will be generated automatically.
You need to have installed laravel and MySQL database. If not, then checkout
First of all, watch the below video which is the look and feel of our example tutorial.
Now, walk through all the below moves to accomplish your goal.
Move 1. Make a fresh laravel project
Create new laravel project by below command
laravel new laraLogin
Move 2. Make Database and change .env file
Now it is time to create a database and connect it with the laravel project.
I am using sequal pro for database management. You might use phpMyAdmin and that is perfectly ok.
Just make a new database and give it a name “registerMysql”
To connect this database with our project, we need to change .env file of project.
Open this .env file and navigate to DB_CONNECTION keyword.
See the below source code
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=registerMysql DB_USERNAME=root DB_PASSWORD=
You need to update above values in your project’s .env file.
I have not any password so I have left it empty.
Move 3. Authentication command
Go to your terminal and hit the below command
php artisan make:auth
This command will create some files automatically. We will see this files later.
Now run the following command
php artisan migrate
When you trigger above command, compiler will connect our database with laravel project and will also create some tables into the database.
Move 4. Generated Files
At this point, we have successfully created our example and you can check registration , login, logout etc. functionalities.
Let us now see which files are automatically generated by compiler.
Navigate to Http->Controller->Auth directory.
Inside Auth folder, you will find some php files like RegisterController.php, LoginController.php, ForgotPasswordController.php etc.
All These files will control the flow of the functionalities. You customize these files as per your requirement.
For example, when user register successfully, below code is redirecting him to home page.
/** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home';
You can edit above code to redirect user to other than home page or any other functionality you want to execute after successful registration.
Similarly, you can edit other controller files.
Move 5. View files
Along with controllers, compiler also have created some blade view files.
Navigate to resources->views->auth directory.
Inside auth folder, you will find some blade files like login.blade.php, register.blade.php etc.
These files are creating design for various pages and you can edit them to make your custom design with html, css and other front end resources.
Now go to the routes->web.php
This file have all the routes definitions.
You will find below line in this web.php file.
Auth::routes();
This one single line includes various routes like after login, after register, after forgot password etc.
To see these routes, hit the below command in your terminal
laraRegisterMysql php artisan route:list
You will see the below output
laraRegisterMysql php artisan route:list +--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | api/user | | Closure | api,auth:api | | | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web,auth | | | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest | | | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest | | | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web | | | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest | | | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest | | | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest | | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest | | | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest | | | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest | +--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+ ➜ laraRegisterMysql
So you can see all the routes in above snippet.
As you have seen that creating a register and login task is very simple in laravel.
You just need to hit one command and create a database.
That is it, all other tasks are automatically.