At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Install and Configure Laravel 5.1 and do simple authentication

  • By Vignesh Thandapani
  • January 5, 2017
  • 4092 Views

Install And Configure Laravel 5.1 and do simple authentication
Here, I am going to explain how to install a fresh Laravel 5.1 package in Ubuntu and how to configure and how to do a simple authentication process in Laravel 5.1.

Install Laravel 5.1 – Server Requirements

The Laravel framework has a few system requirements. Of course, all of these requirements are satisfied by the Laravel Homestead virtual machine:
1. PHP >= 5.5.9
2. OpenSSL PHP Extension
3. PDO PHP Extension
4. Mbstring PHP Extension
5. Tokenizer PHP Extension

Installing Laravel 5.1

Laravel uses Composer to handle all dependencies. You need to install Composer first if composer is not installed. In case the Composer is already installed, skip the below steps and go to step “Install Laravel 5.1 with following command:”.
Composer installation steps for Ubuntu:
(refer: https://getcomposer.org/download/)

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'aa96f26c2b67226a324c27919f1eb05f21c248b987e6195cad9690d5c1ff713d53020a02ac8c217dbf90a7eacc9d141d') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

 
Composer reference for different platforms:
https://getcomposer.org/doc/00-intro.md#manual-installation
Install Laravel 5.1 with the following command:

composer create-project laravel/laravel blog "5.1.*"

 
Provide the permission using below commands for “storage” and “bootstrap”, which are available inside the blog directory, and created.

sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/

 
After the installation is done, create the virtual host and point the document root to “your_application_path/blog/public” since “public” is the document root of the laravel application. This is where the Laravel application will receive all the requests.
Here I am providing how to create virtual host in nginx in ubuntu:
virtual host ref: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts–3

Quick steps for nginx

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/blog-local.com
sudo nano /etc/nginx/sites-available/blog-local.com
/*update the root path*/
root /home/vignesh/projects/blog/public;
sudo ln -s /etc/nginx/sites-available/blog-local.com/etc/nginx/sites-enabled/blog-local.com
sudo service nginx restart
nano /etc/hosts
/*add the below lines in hosts file */
127.0.0.1 blog-local.com

 
That’s it… virtual host created successfully.
Run this url in broswer blog-local.com. You will get the Laravel 5.1 page. Thus the Laravel 5.1 package is installed successfully.
Then create the database “your_db_name” and configure .env file which is available inside the blog directory.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=your_db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password

 
After configuring the database connections I will do the following steps for simple authentication.
Refer: https://laravel.com/docs/5.1/authentication#adding-custom-authentication-drivers
Add the below lines in
app/Http/routes.php

// Providing Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Providing Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

 
Create the view file as given in the link https://laravel.com/docs/5.1/authentication#included-views
Use the above link how to create blade files for authentication process.
Load the url blog-local.com/auth/register
The register form loads, and when you submit the form you will get an error.
Run these below comments in terminal inside your project directory:
(ref : https://stackoverflow.com/questions/36333822/laravel-base-table-or-view-not-found-1146-table-doesnt-exist)

To create the user table:

php artisan migrate

 
After running the above command successfully, Users table will be created in your database with basic columns.
Now do submit auth/register to create first user account.
Basing configure in

app/Http/Controllers/Auth/AuthController.php
/* Will get redirected to this path after successful login */
protected $redirectPath = '/admin/dashboard';
/* Will get redirected to this path if login failed */
protected $loginPath = '/auth/login';

 

Use this command to create the admin controller:

(https://laravel.com/docs/5.1/controllers)

php artisan make:controller AdminController

 
Add this line “return view(‘admin.index’);” in index method of app/Http/Controllers/AdminController.php without the double quotes
and create “admin” directory inside resources and create files index.blade.php. Then add this line in index.blade.php file:

< ?php echo "This is the admin page";?>

 

Add middleware authentication checking for your secured routes:

auth is used to access the particular page for logged in users only, these pages are not accessible to guests.
(https://laravel.io/forum/02-17-2015-laravel-5-routes-restricting-based-on-user-type)

Route::group(['middleware' => 'auth'], function () {
/*
These routes are only accessible by users who can login. Not for guests.
*/
});

 
example:

Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'admin'], function () {
Route::get('/adminindex', [
'uses' => 'AdminController@index',
'as' => 'admin.adminindex'
]);
});
});

 
Thats it… this url will be blog-local.com/adminindex and will be accessible only for logged in users.
Now you can use blog-local.com/auth/login to access the login and submit your created credentials. It will work fine.

Vignesh Thandapani

An enthusiastic Tech Lead with 6 plus years of experience in Web development arena. Owns legitimate experience in CorePHP, Laravel, Symfony, CakePHP, Wordpress, Joomla. Behalf, a young Aspiring "Travel admirer" craves to live with Nature.