Introducing Laravel: Installation and setup

In this introductory series we'll take a look at the PHP framework that Assortment is built on, Laravel, starting with the installation and setup.

By Luke Whitehouse on

LaravelPHP

Tutorial

Laravel is a robust PHP framework created by Taylor Otwell for building websites and applications of all sizes. With a great community behind it and a constant stream of development, it's a safe bet that the framework will be at the forefront of PHP development for the forseeable future.

As version 5.4 has just been released, I thought now would be as good of a time as any to start a series on how you can utilise this framework to build applications of your very own.

When building Assortment I decided at an early stage to go down the route of Laravel rather than a platform that I was more familiar with such as WordPress. Due to their more restrictive nature, a CMS was something I was trying to avoid as I wanted more creative freedom in how I structured my site, which is something I found in Laravel.

In any case, I'll be using my experience of building Assortment and professional work in my role at Mixd to provide an in-depth series of tutorials covering many of Laravel's features and overarching themes.

This is the 1st part of my Laravel series of posts. Check out the others from the links below once they're posted:

Disclaimer

If you're new to the framework or the object-orientated land of PHP then working with Laravel may seem overwhelming at first. There may be some sections of this series that you're not too clued up on but stick with me, I'll be breaking things down into bite-sized chunks which should provide you with those illustrious "a'ha!" moments that everyone craves.

That being said, this series does expect a basic understanding of PHP or similar computer languages.

With that, let's get started.

Installation

We're looking to install the latest version of Laravel, which at the time of this writing is 5.4.

In order to do that, we'll look to install Composer, a dependency manager for PHP.

Installing Composer

Just as Node has NPM and Ruby has Bundler, PHP also has it's own dependency manager called Composer. If you're unfamiliar with dependency managers (or as they're also called – package managers) then think of them as a way to manage the libraries that your application relies on in a way that allows for easy installation and updates moving forward.

Fig 1: The official website of Composer, a dependency manager for PHP
Fig 1: The official website of Composer, a dependency manager for PHP

To download Composer, go to it's download page where you'll find the latest way to install the package. As the installation method for Composer is more secure than most, the instructions are prone to change on each release so you'll need to go through them on the website. At present there are 4 separate php commands that you should run in your Command Line Interface (CLI).

Once you've got your Composer file, go to that folder in your Terminal app (or similar) and move the composer.phar file into your /usr/local/bin directory, which makes the Composer file globally accessible from any directory.

$ cd /path/to/composer.phar/you/just/downloaded
$ mv composer.phar /usr/local/bin/composer

Note: If you have any permission issues, try sudo or switch to a user on your computer that has access to the /usr/local/bin directory.

Fig 2: Running Composer once installed shows it's version and a list of available commands
Fig 2: Running Composer once installed shows it's version and a list of available commands

You can check Composer has been successfully installed by running composer in your Terminal.app, where you'll be shown all the different Composer commands, in addition to the welcome message at the top.

Setting up our Web Server

Before we do anything else, we need to ensure our project is setup in our web server. For this example I'm using a basic LAMP stack, although there are many alternatives to choose from.

Once you've chosen your web server I'd recommend creating a Virtual Host declaration to access your site from, for example lukes-new-app.dev, although you can stick to the default localhost:8888/lukes-new-app should you wish.

Installing Laravel

Now that we have Composer ready and waiting, we can use it to download the latest version of Laravel. In the directory you want to install Laravel on your Web Server, run the composer create-project command in your CLI, whilst remembering the --prefer-dist flag as we only want the latest stable release, rather than a beta version.

$ cd /folder/of/choice
$ composer create-project --prefer-dist laravel/laravel app-name-here

Replace app-name-here with the name of your new application. Composer will then install your chosen version of Laravel and all it's dependencies.

It may take a little while, go make a brew.

Once installed, view your project in the browser and you should now see Laravel's splash screen.

Fig 3: The Laravel 5.4 splash screen that is shown once successfully installed
Fig 3: The Laravel 5.4 splash screen that is shown once successfully installed

Note: This splash screen may be different depending on the version of Laravel you're installing.

Configuration

Now that we have our Laravel application installed, there's a few things we're going to want to configure for the next post in the series.

Laravel uses the PHP Dotenv shim to load environment variables throughout it's projects. As this file will be ignored by Git, it's the perfect place to store sensitive information about your application such as database credentials and passwords. If you'd like to read more into this convention then the PHP Dotenv Github is a perfect place to get started.

Located in the root of your project, open up the .env file in a code editor. We needn't concern ourselves with everything in this file just yet, but for now change APP_URL to the chosen URL of your project, whether that's http://lukes-new-app.dev, http://localhost:8888/lukes-new-app or something different.

In addition, we need to create a database and then add it's connection details to the .env file. Within the file, locate the DB_* section and fill in as appropriate.

  • DB_CONNECTION - This is the type of database system you're running. Laravel defaults to mysql, which we'll be using for this series, but feel free to read up on how to change that.
  • DB_HOST - The hostname for your database connection, if you don't know this it's more than likely 127.0.0.1 or localhost.
  • DB_PORT - The port for your database connection, if you don't know this it's more than likely 3306.
  • DB_DATABASE - The name of your database. I like to use underscores for spaces in my names and prefix them with the type of project it is, so I'll go with lara_lukes_new_app.
  • DB_USERNAME - The username in MySQL that has permissions to access your database. Default for localhost is usually root.
  • DB_USERNAME - The password of your user. Default for root is usually root.

Note: Do not leave your username and password as root for live websites, that is incredibly insecure. Ideally you would have a separate user for each database, each only able to access it's correlating database and no others.

Here's how those fields now read for me.

APP_URL=http://lukes-new-app.dev

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Moving out of the .env file, locate the config/app.php file and find the name value and change it to the name of your blog, in this case I'll change mine to lukes-new-app.

Done that? Awesome, Laravel is now successfully installed with some basic configuration and is ready for development. In the next post, we'll look at the concept of Routing and Controllers to create some basic pages for a dummy blog.

I'm eager to start making more tutorials on Laravel so if you have any suggestions for one-off posts or a more in depth series then please let me know.

Until next time 

Follow us on Twitter, Facebook or Github.
© Copyright 2021 Assortment.
Created and maintained by Luke Whitehouse.