net tutsplus com tutorials php authentication with laravel 4

Download Net Tutsplus Com Tutorials Php Authentication With Laravel 4

Post on 25-Oct-2015

262 views

Category:

Documents

4 download

Embed Size (px)

DESCRIPTION

Net Tutsplus Com Tutorials Php Authentication With Laravel 4.

TRANSCRIPT

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    94,514Twitter Followers

    80,346Facebook Fans

    21,883Google+ Followers

    Tutorial Details

    Difficulty: Intermediate

    Estimated Completion Time: 30

    Minutes

    View post on Tuts+ Beta

    Tuts+ Beta is an optimized,mobile-friendly and easy-to-readversion of the Tuts+ network.

    Andrew Perkins on Nov 11th 2013 with 64 Comments

    Authentication With Laravel4

    Tutorials \ PHP \ Rating:

    Tweet Like 144

    Download SOURCE FILES

    Authentication is required for virtually any type of web application. In this tutorial, Id like to show you how

    Search...

    Tutorials Articles Tips Sessions Resources Videos Tuts+ Premium Website Templates

    Advertise Write for Us About

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Advertise Here

    advertise here

    Subscribeme@email.com

    Full-time, Part-time and Contract Jobs

    PHP Developer (MVC/EE/Laravel/etc...) at Vector Media Group

    Tuts+ CMS & WordPress Editor at Envato

    Tuts+ Mobile Dev Editor at Envato

    Software Developer at Booking.com

    Application Developer - Android OS at Booking.com

    More on Tuts+ Jobs...

    you can go about creating a small authentication application using Laravel 4. Well start from the verybeginning by creating our Laravel app using composer, creating the database, loading in the TwitterBootstrap, creating a main layout, registering users, logging in and out, and protecting routes using filters.Weve got a lot of code to cover, so lets get started!

    InstallationLets start off this tutorial by setting up everything that well need in order to build our authenticationapplication. Well first need to download and install Laravel plus all of its dependencies. Well also utilize thepopular Twitter Bootstrap to make our app look pretty. Then well do a tad bit of configuration, connect toour database and create the required table and finally, start up our server to make sure everything isworking as expected.

    DownloadLets use composer to create a new Laravel application. Ill first change directories into my Sites folder as

    thats where I prefer to store all of my apps:

    Then run the following command to download and install Laravel (I named my app laravel-auth) and all of

    its dependencies:

    Add In Twitter BootstrapNow to keep our app from suffering a horrible and ugly fate of being styled by yours truly, well include theTwitter bootstrap within our composer.json file:

    1 cd Sites

    1 composer create-project laravel/laravel laravel-auth

    123456

    { "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "require": { "laravel/framework": "4.0.*",

    Get Email Updates Subscribe via RSS

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Freelance Jobs

    iOS App Developer in Anywhere

    .NET Form Submission and data capture using JSON in USA

    Wordpress Developer with Good Design Skills in Anywhere

    Ruby on Rails developer in Anywhere, Remote

    Code a mobile site and app in Anywhere

    More Freelance Jobs...

    Find us on Facebook

    Nettuts+

    Like

    80,348 people like Nettuts+.

    Facebook social plugin

    and then we can install it:

    Now if you open up your app into your text editor, Im using Sublime, and if you look in the vendor folder

    youll see we have the Twitter Bootstrap here.

    Now by default our Twitter Bootstrap is composed of .less files and before we can compile them into .CSS

    files, we need to install all of the bootstrap dependencies. This will also allow us to use the Makefile that is

    included with the Twitter bootstrap for working with the framework (such as compiling files and running

    678910

    "laravel/framework": "4.0.*", "twitter/bootstrap": "*" }, // The rest of your composer.json file below ....

    1 composer update

    Facebook Twitter Google+

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    tests).

    Note: You will need npm in order to install these dependencies.

    In your terminal, lets change directories into vendor/twitter/bootstrap and run npm install:

    With everything ready to go, we can now use the Makefile to compile the .less files into CSS. Lets run

    the following command:

    You should now notice that we have two new folders inside our vendor/twitter/bootstrap directory

    named bootstrap/css which contain our bootstrap CSS files.

    12

    cd ~/Sites/laravel-auth/vendor/twitter/bootstrapnpm install

    1 make bootstrap-css

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF APINow we can use the bootstrap CSS files later on, in our layout, to style our app.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    But, we have a problem! We need these CSS files to be publicly accessible, currently they are located in ourvendor folder. But this is an easy fix! We can use artisan to publish (move) them to our

    public/packages folder, that way we can link in the required CSS files into our main layout template, which

    well create later on.

    First, well change back into the root of our Laravel application and then run artisan to move the files:

    The artisan command asset:publish allows us to provide a --path option for which files we want to move

    into our public/packages directory. In this case, we tell it to publish all of the CSS files that we compiled

    earlier and place them inside of two new folders named bootstrap/css. Your public directory should now

    look like the screenshot below, with our Twitter Bootstrap CSS files now publicly accessible:

    12

    cd ~/Sites/laravel-authphp artisan asset:publish --path="vendor/twitter/bootstrap/bootstrap/css" bootstrap

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Set PermissionsNext we need to ensure our web server has the appropriate permissions to write to our applicationsapp/storage directory. From within your app, run the following command:

    1 chmod -R 755 app/storage

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Connect To Our DatabaseNext, we need a database that our authentication app can use to store our users in. So fire up whicheverdatabase you are more comfortable using, personally, I prefer MySQL along with PHPMyAdmin. Ive createda new, empty database named: laravel-auth.

    Now lets connect this database to our application. Under app/config open up database.php. Enter in

    your appropriate database credentials, mine are as follows:

    1234567891011121314

    // Default Database Connection Name 'default' => 'mysql', // Database Connections 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'laravel-auth', 'username' => 'root', 'password' => '',

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Create the Users TableWith our database created, it wont be very useful unless we have a table to store our users in. Lets useartisan to create a new migration file named: create-users-table:

    Lets now edit our newly created migration file to create our userstable using the Schema Builder. Well

    start with the up() method:

    This will create a table named users and it will have an id field as the primary key, firstname and

    lastname fields, an email field which requires the email to be unique, and finally a field for the password

    (must be at least 64 characters in length) as well as a few timestamps.

    Now we need to fill in the down() method in case we need to revert our migration, to drop the users table:

    And now we can run the migration to create our users table:

    Start Server & Test It Out

    14151617181920

    'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), // the rest of your database.php file's code ...

    1 php artisan migrate:make create-users-table

    123456789

    public function up(){ $table->increments('id'); $table->string('firstname', 20); $table->string('lastname', 20); $table->string('email', 100)->unique(); $table->string('password', 64); $table->timestamps();}

    1234

    public function down(){ Schema::drop('users');}

    1 php artisan migrate

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Alright, our authentication application is coming along nicely. Weve done quite a bit of preparation, letsstart up our server and preview our app in the browser:

    Great, the server starts up and we can see our home page:

    Making the App Look PrettyBefore we go any further, its time to create a main layout file, which will use the Twitter Bootstrap to give ourauthentication application a little style!

    Creating a Main Layout File

    1 php artisan serve

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Under app/views/ create a new folder named layouts and inside it, create a new file named

    main.blade.php and lets place in the following basic HTML structure:

    Linking In the CSS FilesNext, we need to link in our bootstrap