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:
- Routing, Blade and Laravel Mix (2nd March 2017)
- Databases and migrations (coming soon)
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.
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.
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.
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
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.
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
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.
Note: This splash screen may be different depending on the version of Laravel you're installing.
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://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
DB_PORT- The port for your database connection, if you don't know this it's more than likely
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
DB_USERNAME- The username in MySQL that has permissions to access your database. Default for localhost is usually
DB_USERNAME- The password of your user. Default for
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
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 🍵
#Get permalink Related posts
Like what you've seen? Want more? Check out similar posts on Assortment that you may find interesting:
#Get permalink Comments
Nice tutorial, Luke!
One other thing that you need to do after the installation of Laravel is to set permissions on the storage and bootstrap/cache directories in the Laravel directory so that they are writable by Laravel. For example: sudo chmod -R +w storage bootstrap/cache
Looking forward to other articles in the series. Tim https://github.com/timothydjones/awesome-laravel
Thanks for taking your time to respond to this post and for mentioning the permissions issues with the current setup. It's something I am aware of although I didn't want to overburden newer people looking to get into frameworks such as Laravel. Instead, I plan to mention this in a future post when we directly need it. Thanks again though!
Nice article, thank you! An easy way to try out Laravel in action is to use Laravel 5 Boilerplate / Starter Kit - https://github.com/Labs64/laravel-boilerplate This is also offering Docker container, whit this you don’t need a local PHP (composer, node.js, etc.) environment.
laravel development company
Nice Article! Thanks for sharing with amazing content.