How to Create Laravel Project with Authentication

Posted on

This is an article which is actually written to create a laravel web-based application project where the application itself does not only contain regular running default laravel web-based application project but also another additional feature which in the context of this article it is an authentication feature.

To be able to achieve it which means having a running laravel web-based application project, below are steps to accomplish it :

1. Create a Laravel web-based application project. There are several references exist in this website concerning on how to install a Laravel web-based application project. An article titled ‘How to Install Laravel to a certain directory‘ and also another article titled ‘How to Install a Specific Version of Laravel using Composer‘ can be visited for further references.

Normally the command to be executed in order to create the laravel web-based application project is by typing the following command into the command line or bash prompt interface :

composer create-project laravel/laravel folder_name

Don’t forget to install composer to be able to run the above command.

2. Test the laravel web-based application by executing the following command :

user@hostname:/var/www/html/laravel-authentication$ php artisan serve
Laravel development server started: <http://127.0.0.1:8000>

After executing the above command, try to access it from the web browser typing the URL address specified as in http://127.0.0.1:8000 which can be viewed in the following image :

How to Create Laravel Project with Authentication

3. The next step is for having the authentication feature added or implemented to the already installed or generated Laravel web-based application project folder. In order to provide the authentication feature or function, Laravel has already provided a baseline feature which can be added for authentication purpose by executing the following command :

php artisan make:auth

Try to execute the above command in the command line specifically inside the Laravel web-based application project’s folder :

user@hostname:/var/www/html/laravel-authentication$ php artisan make:auth
Authentication scaffolding generated successfully.
user@hostname:/var/www/html/laravel-authentication$

After executing the above command, try to revisit the URL address specified before which is : http://127.0.0.1:8000. The following is the image of the page. Try to search the difference with the previous display where it is obviously in the image below there are two additional links added. Those links are LOGIN and REGISTER links which is located in the right top of the page as shown below :

How to Create Laravel Project with Authentication

If the link is clicked, below is the LOGIN link as shown below :

How to Create Laravel Project with Authentication

The other one is the REGISTER link as shown below :

How to Create Laravel Project with Authentication

The changes can be found in the following files :

1. The web.php file as the main routing configuration file used by Laravel which is located in the folder routes/ inside the root folder of a Laravel web-based application project.

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');

2. A controller file named HomeController.php created inside the folder of ‘/app/Http/Controller’ from the root folder of a Laravel web-based application project. It can be seen as follows :

user@hostname:/var/www/html/laravel-authentication/app/Http/Controllers$ pwd
/var/www/html/laravel-authentication/app/Http/Controllers
user@hostname:/var/www/html/laravel-authentication/app/Http/Controllers$ ls -al
total 20
drwxrwxr-x 3 www-data user 4096 Nov  3 10:47 .
drwxrwxr-x 4 www-data user 4096 Aug 30 16:55 ..
drwxrwxr-x 2 www-data user 4096 Aug 30 16:55 Auth
-rw-rw-r-- 1 www-data user  361 Aug 30 16:55 Controller.php
-rw-rw-r-- 1 user     user  452 Nov  3 10:47 HomeController.php
user@hostname:/var/www/html/laravel-authentication/app/Http/Controllers$

Below is the content of the file named HomeController.php :

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}

3. Another file named home.blade.php which is located inside the folder of ‘/resources/views’ placed in the root folder of a Laravel web-based application project. Below is the content of the file named ‘home.blade.php’ :

@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
You are logged in!
</div>
</div>
</div>
</div>
</div>
@endsection

Below is the exact location of the file :

user@hostname:/var/www/html/laravel-authentication/resources/views$ ls -al
total 24
drwxrwxr-x 4 www-data user 4096 Nov 3  11:08 .
drwxrwxr-x 5 www-data user 4096 Aug 30 16:55 ..
drwxr-xr-x 3 user user     4096 Nov 3  10:47 auth
-rw-rw-r-- 1 user user      624 Nov 3  10:47 home.blade.php
drwxr-xr-x 2 user user     4096 Nov 3  10:47 layouts
-rw-rw-r-- 1 www-data user 2736 Aug 30 16:55 welcome.blade.php
user@hostname:/var/www/html/laravel-authentication/resources/views$

Besides home.blade.php there are also two additional folders created for the purpose of showing the login, register page and its additional layout.

2 thoughts on “How to Create Laravel Project with Authentication

Leave a Reply