Basic Authentication With Laravel 5.5
Hi people, Laravel makes implementing authentication very simple. In this tutorial, I am going to be taking you through setting up basic authentication in laravel.
Want to get started fast? then this tutorial is for you!!! I would also advice that after this tutorial, you should go into the code to actually understand what Laravel does for you underneath, I would try to explain as much as i can.
- Make sure you have composer installed, if not head to https://getcomposer.org/ and
- Mysql database installed
- Make sure you have created a Laravel application, you may install Laravel by issuing the Composer
create-project
command in your terminal.
composer create-project --prefer-dist laravel/laravel project_name "5.5.*"
The next thing you should do after installing Laravel is to set your application key with this command: php artisan key:generate
The key can be set in the .env
environment file. If you have not renamed the .env.example
file to .env
, you should do that now. Also, set values for your DB
Run php artisan serve
, you should see this on your terminal —
Laravel development server started: <http://127.0.0.1:8000>
Follow the URL above to see Laravel’s default page.
Now, run php artisan make:auth
and php artisan migrate
. These two commands will take care of scaffolding your entire authentication system!
Then, navigate your browser to http://127.0.0.1:8000/register
or any other URL that is assigned to your application.
Now i am about to dive into what those two commands actually help you do, Just hang in!!!!
Laravel ships with several pre-built authentication controllers, which are located in the App\Http\Controllers\Auth
namespace. The RegisterController
handles new user registration, the LoginController
handles authentication, the ForgotPasswordController
handles e-mailing links for resetting passwords, and the ResetPasswordController
contains the logic to reset passwords. Each of these controllers uses a trait to include their necessary methods. For many applications, you will not need to modify these controllers at all. You should check through the controllers, alot of amazing stuff were done in it
php artisan make:auth
- This command will install a resources/views/layouts directory containing a base layout for your application. You can edit to however you want it.
- It would also install a resources/views/auth directory containing the registration, login and reset-password pages, you are also free to edit these pages to your taste.
- In routes/web.php(this file holds all routes you are going to be using in your application) it adds a line Auth::routes(); that handles all authentication routes. To see the actual routes, their HTTP method and the controller method they call, you can run
php artisan route:list
on your terminal. - It also creates migration files in database/migration directory. Migrations are files which contain a class definition with both an
up()
and adown()
method. Theup()
method is run when the migration is executed to apply changes to the database. Thedown()
method is run to revert the changes. read more about migrations here.
php artisan migrate
- This command helps run all your migration files.
This is a very basic usage and explanation of Laravel’s out-of-the-box Authentication. Need more insight or deeper understanding? you can head over to Laravel’s official documentation.
Kindly share this article to others if you think it’ll be helpful and also ask me questions in the response section. Thanks!!!!!