User Authentication in Laravel 4.x: Simple Guide Part 2

Part 1: User Authentication in Laravel 4.x: Simple Guide Part 1

user authentication in laravel 4 part 2
User Authentication In Laravel Screenshot

Let’s see what we have covered so far in the first part of this tutorial:

  • Database Creation And Configuration
  • Migrations
  • Seeding
  • Controllers

Now we are left with Routing and Views, let’s start with routes first.

Defining Routes

Since we are using restful controllers in our application, we will be using Route::controller() method for registering routes. Route::controller() allows us to define all the routes to a restful controller’s actions using a single call per controller. So, we will add the following two lines in our app/routes.php file to register controller routes:


Laravel routes are FIFO ( First In First Out). So, the order of the above Route::controller() calls is important.

Creating Views

user authentication in laravel 4 part 2
app/views Directory Screenshot

Master Layout

Master layouts in Laravel are used to define a default layout that will be used by all the views in an application. The master layout for our application looks like this:

<!-- file: app/views/master.blade.php -->




<!-- file: app/views/header.blade.php -->

<!DOCTYPE html>
<html class="no-js" lang="en">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Larave 4 Authentication Tutorial</title>
    {{ HTML::style('assets/css/foundation.css') }}
    {{ HTML::script('./assets/js/vendor/custom.modernizr.js') }}

<!-- file: app/views/footer.blade.php -->

    {{ HTML::script('./assets/js/vendor/jquery.js') }}
    {{ HTML::script('./assets/js/foundation.min.js') }}


@include directive is a part of Laravel’s Blade Template Engine. We use @include directive to include sub-views in a parent or master view. So, here we used @include to render header and footer as a part of master view. In header.blade.php, I have used HTML::style() and HTML::script() to link Foundation’s stylesheet and scripts ( see my article on ZURB’s Foundation ). This is not a requisite for this tutorial.

NOTE 1: HTML::style() and HTML::script() methods use public directory’s path ( Web Root) to generate link to assets ( stylesheets, scripts ). So, we place our assets inside public directory to make this work.

NOTE 2: To use a master layout with our controllers, we are required to add protected $layout = 'master'; in our BaseController.

Index View

<!--file: app/views/index.blade.php-->

<div class="row">
    <div class="large-12 column">
        <div class="panel callout radius">
            <h1> Laravel 4 Authentication Tutorial </h1>
        <p> Please {{ HTML::linkAction('Admin\AdminController@getLogin','Login') }} to access dashboard</p>

Login Form

<!--file: app/views/admin/login.blade.php-->

<div class="row">
    <div class="large-6 large-centered column">
            {{ Form::open(['action' => 'Admin\AdminController@postLogin']) }}
                {{ Form::label('username','Username') }}
                {{ Form::text('username',Input::old('username'),['placeholder'=>'Your nice name']) }}
                {{ Form::label('password','Password') }}
                {{ Form::password('password',['placeholder'=>'Password here']) }}
                {{ Form::submit('Login',['class'=>'button tiny radius']) }}
            {{ Form::close() }}
                @foreach ($errors->all() as $message) 
                    <span class="label alert round">{{$message}}</span><br><br>
                <span class="label alert round">{{Session::get('failure')}}</span>


A short Form helper reference:

Method Description
open() Opens a form.
label() Generates a label element.
text() Generates a text field.
password() Generates a password field.
submit() Generates a form submit button.
close() Closes a form

For further details and documentation, please visit Laravel’s official documentation page on HTML and Forms.


user authentication in laravel 4 part 2
Index page screenshot
user authentication in laravel 4 part 2
Login page with validation errors
user authentication in laravel 4 part 2
Login page with worng username/password screenshot

Thank you for reading this article. If you have any suggestions, please leave a comment.