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:

Route::controller('admin','Admin\AdminController');
Route::controller('/','IndexController');

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 -->

@include('header')

    @yield('content')

@include('footer')

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

<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
    <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') }}
</head>
<body>

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

    {{ HTML::script('./assets/js/vendor/jquery.js') }}
    {{ HTML::script('./assets/js/foundation.min.js') }}
    <script>
          $(document).foundation();
    </script>

</body>
</html>

@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-->

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

Login Form

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

@section('content')
<div class="row">
    <div class="large-6 large-centered column">
            {{ Form::open(['action' => 'Admin\AdminController@postLogin']) }}
            <fieldset>
                <legend>Login</legend>
                {{ 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']) }}
            </fieldset>
            {{ Form::close() }}
            @if($errors->has())
                @foreach ($errors->all() as $message) 
                    <span class="label alert round">{{$message}}</span><br><br>
                @endforeach
            @endif
            @if(Session::has('failure'))
                <span class="label alert round">{{Session::get('failure')}}</span>
            @endif

    </div>
</div>
@stop

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.

Screenshots:

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.