Bootstrap a new Laravel project with user authentication (in 5 minutes)

11 Mar 2017 in Tech

We're starting from scratch here, so the first thing we need to do is to create a basic Laravel installation for our project. To to this, run the following command:

bash
composer create-project --prefer-dist laravel/laravel demo

This will create a new folder called demo with a Laravel application inside it. We can change to the demo directory and run php artisan serve before visiting http://localhost:8000 in a browser to make sure that it worked correctly. You should see the word "Laravel" and links to the documentation page, Laracasts, Forge and GitHub.

As we're going to be working with user authentication we'll need to configure a few more things too, such as our database. Edit the .env file and change the values for the DB_* entries so that they connect to your database. For me, they look something like this:

ini
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=demo
DB_USERNAME=mydemouser
DB_PASSWORD=supersecretpassword

You'll also need to create the database for Laravel to use with the following command (you may need to edit it to provide the root password):

bash
mysql -u root -e "CREATE DATABASE demo;"

Once we have our database set up, it's time to start thinking about authentication. As it's such a common part of most applications, Laravel makes it really easy to get started - in fact it's as easy as running a single command:

bash
php artisan make:auth

The Laravel documentation explains what this does perfectly:

This command should be used on fresh applications and will install a layout view, registration and login views, as well as routes for all authentication end-points. A HomeController will also be generated to handle post-login requests to your application's dashboard.

As well as generating all of those files, Laravel will create a database migration for us (in the database/migrations folder), so we need to run php artisan migrate to make sure that the relevant database tables are created too.

Once this is done, you'll notice some new Login and Register links in the top right corner when you visit http://localhost:8000/ in your browser. Click on Register and fill in your details to create a new account with our application. Once your registration is complete, you'll be redirected to the home controller which will show you a message, telling you that you're logged in!

That's really all there is to it - five steps:

  1. Use composer to install Laravel
  2. Edit the .env file
  3. Create a database for our application
  4. Run php artisan make:auth
  5. Run php artisan migrate

Laravel makes it super easy to get a multi-user application up and running. The documentation is great and they've really distilled it down to the minimum number of steps. I've used lots of applications in the past, but Laravel is definitely the easiest to get to this point with.