Working with PHP for the past 10 years or so has taken me through quite a ride. Starting with rookie mistakes like not being able to find a semicolon to trying to build my own framework for building sites (do not do this), I finally arrived at something great when I learned object-oriented PHP.
Almost all CMS systems use it, including Joomla, Drupal and WordPress, and of course many frameworks like CakePHP use it as well. I arrived at Laravel in early 2014 and I was instantly hooked. It not only uses OOP, it requires it! I have to admit, this was a challenge, but I learned so much thanks to it, and it has made the code I write for other systems a lot better too!
In this article I’ll show you how to get started with Laravel by installing everything that’s needed to run the system, showing you how to use Laravel Homestead—a great virtual machine—and finally, how to install Laravel itself.
It may seem like a mouthful, but I wanted to make sure that everyone can install it, whether they’re on Windows, Linux or Mac, and even if they have few of the prerequisites. Don’t worry, you only need to do most of this stuff once, and even if you need to do it again, it will be much simpler the next time.
Laravel is a PHP Framework created by Taylor Otwell as a more advanced take on the CodeIgniter framework. The first beta was released in mid-2009. As of June 2015, Laravel is at version 5.1, which is the first version to receive LTS (Long Term Support), putting Laravel into the mix when searching for a viable platform for large-scale work.
Laravel is designed to help you work in a standardized, elegant and rapid fashion. This is achieved through strict guidelines and third-party systems.
Laravel has a strict Model-View-Controller (MVC) structure and a natural way of working with object-oriented code. These two features almost force a certain way of writing code on developers—which is a good thing.
As far as style and methodology goes, Laravel applications will be similar, so other developers can come in and work with an instantly familiar mindset.
Laravel’s elegance stems from the same OOP nature and MVC mindset and the routing methodology used. A few simple characteristics of the engine as a whole conspire to make you write code which is visually pleasing, organized, and as self-documenting as possible.
Self-documentation is a wonderful byproduct of well-written object-oriented code. You can achieve almost readable sentences by naming your classes, functions and variables properly, adding up to highly browsable code.
More importantly, all this translates to predictable and testable code which produces consistent, easy-to-foresee results every time.
The speed at which you can build an application is astonishing. If you are new to OOP, Composer and using the terminal, this may not be apparent at first, but once you get the hang of things you will be astonished at what you can accomplish.
Laravel is set up to work well with third-party packages from Composer, which means that adding full support for the Facebook API, or the MailChimp API, Loggers, Profilers and other tools takes a few keystrokes—not minutes or hours.
You can install Laravel on most servers, or create your own using a virtual machine. The basic requirements are:
I’m using Laravel Homestead to run Laravel locally. Homestead is a Vagrant box which you can grab for free. The link above has complete installation instructions if you need them, but I’ll outline the main steps below.
In addition to a server, you’ll need to have Composer installed, which is a package manager. Laravel uses it to download any third-party code it needs.
While git is not strictly necessary, it will make your life a lot easier. Git is a versioning system, and lots of people use it for their projects, hosting them on GitHub. To be able to use git effectively, you’ll need the git command, which we’ll install below.
Composer can be installed locally or globally. I prefer the global option because it makes my life a lot easier, so that’s what I’ll show you here. For more detailed information and instructions for different OS and setups, take a look at the Getting Started Guide.
Linux and OSX users should first make sure that the
/usr directory exists, and if it doesn’t, create it. Next, run the following command:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Windows users can use the installer from the Getting Started Guide. If you want to be able to run the
composer command from anywhere, you need to place it in your path.
To place a folder in your path, just follow these instructions from WindowsITPro.
Luckily, git is super-simple to install, so just take a look at the Getting Started guide for detailed instructions.
On Linux, you’re most likely to be able to install it with the following simple command.
sudo yum install git-all
On OSX, the easiest way to get git support is to install the Xcode Command Line Tools, or to go to the Git Download page and grab the latest version.
For Windows, head on over to the Git Download page and run the latest installer.
The box contains the operating system and other software you’ll need to run a local server and Laravel. You can download the box with a single command in your terminal.
vagrant box add laravel/homestead
It may take a while for the box to download, so feel free to grab a coffee at this point.
To install the command-line interface for Homestead, you’ll need to clone a git repository. Since we now have git installed, we can use a single command. Run the following in a folder where you want to keep all your Laravel projects.
git clone https://github.com/laravel/homestead.git Homestead
I work on OSX and I have a folder within my main user home folder named “websites”. Within that folder I have a “wordpress” folder, which contains WordPress projects that run from a different Vagrant box. I ran the command above from within the “websites” folder. This created a “Homestead” folder in there, and all my Laravel projects will be placed within.
If you want to be on the bleeding edge of PHP, you can install the PHP 7 version of Homestead instead. Use the following command:
git clone -b php-7 https://github.com/laravel/homestead.git Homestead
To initialize Homestead, run
bash init.sh from within the Homestead folder, which will create the appropriate files in the
~/.homestead directory within your home directory.
The first thing we’ll need to do is create an SSH key and add the path to it to the
Homestead.yaml file. To create an SSH key on Linux or OSX, use the following command:
ssh-keygen -t rsa -C "yourname@homestead"
On Windows, the easiest way to create this is to use the Git Bash tool. This is similar to the terminal or Command Prompt but has some added awesome. Switch it on by right-clicking the desktop and clicking Git Bash.
The next step is to modify the
~/.homestead/Homestead.yaml file. Now there’s actually not a lot we need to do because it should already be set up almost perfectly. If you open it up, you should see why:
virtualbox, which is what we’re using.
~/.ssh/id_rsa.pub, which should be where your SSH key was saved. Take a look at the messages received when you generated your SSH key, and you should see the path in there.
192.168.10.10. You can change it if you’d like to, but we’ll be working with this.
In fact, all we’ll do is modify the sites section and set the shared folder. It would work just fine as it is, but we’ll be running a number of projects, so it’s best if you already know how to create them. Let’s create two test sites, a blog and a webshop, which we want to reach through
Make sure that your sites definition in the file looks like this:
sites: - map: store.local to: /home/vagrant/Code/store/public - map: blog.dev to: /home/vagrant/Code/blog/public
The last step here is to add the domains to the hosts file on your computer. On Linux and OS X, this file is located at
/etc/hosts, and on Windows it is at
C:\Windows\System32\drivers\etc\hosts. You need to add two lines to this file:
192.168.10.10 store.local 192.168.19.10 blog.dev
Finally, to set the folder(s) shared between the virtual machine and your local machine, look to the folders section. I’ve set it up like so:
folders: - map: ~/websites/Homestead/Code to: /home/vagrant/Code
This will be pretty short! Go to the Homestead folder and run
vagrant up. The first time you do this it may take a while, but subsequent launches will be extremely fast. The first time round an OS is essentially installed and configured, and the next time round it just needs to be booted up.
If you’ve done everything correctly, you should be able to visit
http://store.local and get the message “No input file specified”. This is fine—it just means our folder is empty. If you go into your
~/websites/Homestead/Code folder and create the
store/public folder and an
index.php in it with some content, it will render just fine. At this stage you can start building your website there!
Everything you just did needs to be done only once, except for typing
vagrant up to start the server. Don’t worry if creating a Homestead instance isn’t in your blood yet, because for now you just need to know how to use it.
Any time you stop your server using
vagrant halt or switch off your computer, you’ll need to use the
vagrant up command to start the server. There are a few things you’ll want/need to do, so let’s look at some of the day-to-day tasks.
For some more advanced configurations you’ll need to SSH into the virtual machine. You can do this by navigating to the Homestead directory in your terminal and typing
vagrant ssh. This will bring you into the machine and you can set things up here if needed.
You can connect to databases using the
homestead username and
secret password. If you’re in the terminal you can use the
mysql -u homestead -p command and supply your password to get command-line access.
If you use an application like Sequel Pro, you should use
127.0.0.1 as the host, your username and password, and
33060 as the port.
Our first task is to download the Laravel installer. Issue the following command in the terminal:
composer global require "laravel/installer"
You should now be able to type
laravel in the command line to access a bunch of great options for working with Laravel. If it doesn’t seem to work, you’ll need to place the
~/.composer/vendor/bin directory in your path. To do that, you’ll need to open your
~/.bash_profile file—which one depends on your setup. Once in there, add the following line to the file:
echo 'export PATH="$PATH:~/.composer/vendor/bin"' >> ~/.bashrc
To see if it worked, type
source ~/.bashrc, or close and reopen the terminal window and try typing
laravel again. If you’re a Windows user you’ll need to follow the instructions here.
store folder we set up that holds the code for our upcoming store? I went back, deleted that folder and used the terminal to navigate to the parent folder
~/websites/Homestead/Code. While there I typed
laravel new store and just like that, a Laravel project was set up for me. Visiting
http://store.local I now see this:
In the next articles about Laravel, I’ll show you how to get started building something as a beginner. Laravel is based strongly around object-oriented PHP, so I recommend you take a look at the excellent OOP For Beginners article by Jason Lengstorf. I’ll explain as much as I can, but if you familiarize yourself with the concepts in that article first, everything will go more smoothly.