Laravel: Creating a Simple Login Form

Laravel has been one of the widely chosen web application frameworks used for creating web applications. As a matter of fact, the popularity of Laravel web application development, which is an open-source framework has been increasing with time. This PHP-based framework can be used for result-oriented and maintainable code.

In the previous blog, we learned how to create a user registration form using Laravel and save details to the database. Now let us see how to create a simple login form using it.

Create a page login.blade.php in ‘app/views’ directory and place this code:

<h1>Sign In</h1>

@if ($errors->any ())


{{implode ('', $errors->all ('<li>: message</li>'))}}



{{Form::open (array ('url' => 'logincheck'))}}

<p> {{Form::text ('username', ‘‘, array ('placeholder'=>'Username','maxlength'=>30))}} </p>

<p> {{Form::password ('password', array('placeholder'=>'Password','maxlength'=>30)) }} </p>

<p> {{Form::submit ('Submit')}} </p>

{{Form::close ()}}

This will create a simple login form with action = ‘logincheck’ and method=’POST’, this form asks only username and password.

We have placed a message section above the form to notify upon error. This section will check if there is any item in ‘errors’ array (‘errors’ array is set from login action using ‘withErrors’ given below).

Now add a route to access this page

Open ‘ app/routes.php’ and add

Route::get ('login',function(){

Return View::make ('login');


Now, we have to add a new route for ‘logincheck’ in our ‘ app/routes.php’, so add these lines of code

Route::post('logincheck', function(){

// username and password validation rule

$rules = array (

'username' => 'required|max:25',

'password' => 'required|max:25',


$v = Validator::make (Input::all (), $rules);

if ($v->fails()) {

// username or password missing

// validation fails

// used to retain input values

Input::flash ();

// return to login page with errors

return Redirect::to('login')


->withErrors ($v->messages ());

} else {

$userdata = array (

'username' => Input::get('username'),

'password' => Input::get('password')


If (Auth::attempt ($userdata)) {

// authentication success, enters home page

return Redirect::to('home');

} else {

// authentication fail, back to login page with errors

return Redirect::to('login')

->withErrors('Incorrect login details');




After login success page redirects to the url ‘/home’, so we have to create a view for that.

Create a page home.php in ‘app/views’ directory and place this code:

<h1>Home Page</h1>

<h5>Home page contents</h5>

Now open ' app/routes.php' and add a route to this file

Route::get ('home',function(){

return View::make('home');


In this example, we used Auth::attempt method (laravel built-in function) to authenticate user credentials.

NOTE: you don’t need to hash the password while using the Auth::attempt method, because by default ‘attempt’ method will hash the password

You can use Auth::check () method to check whether the user is logged or not, Auth::check () will return TRUE if logged and FALSE if not logged.

Do you have a set of routes that need to be accessed only by logged users? You can check whether the user has logged before going to a route using ‘auth’, i.e.:

Route::group (array ('before' => 'auth'), function () {

// list routes that needs authentication


In our example we can place our ‘home’ route inside this, now it might look like:

// check if logged for a set of routes

Route::group (array ('before' => 'auth'), function () {

Route::get ('home',function(){

return View::make('home');



Now, when a user tries to access the home page, Laravel, checks whether he is logged. If yes, goes to the desired route.

If checking fails redirects to the URL defined in the Redirect::guest () given in ‘Authentication Filters’ section in ‘app/filters.php’.

One more function needs to be added, i.e., if logged, the user needs to enter the home page upon accessing login page.

For this, we need to add a simple authentication checking in the login page route. The new login route looks like this:

Route::get (‘/login’,function(){

// if logged redirect to home page

If (Auth::check ()) {

return Redirect::to('/home');


return View::make('login');


At last, let us see how to log out from the current session.

Create a link for logout

{{link_to('/logout','Logout')}} and place it in the home page (header of logged pages).

this is same as <a href="/logout">Logout</a>

Then add route for logout action, it is very simple as given below:

Route::get ('logout',function(){

Auth::logout ();

return Redirect::to('login');


This clears logged user session and then returns to the login page.

This is a very simple blog just to show how the Laravel built-in authentication functions (Auth::attempt, Auth::check, Auth::logout) work. Hope you all got an idea about creating a login form using Laravel.

Know More About Laravel

Latest Post