Apr 29, 2013
Table of contents:
Laravel 4 is the upcoming latest release of the popular PHP framework Laravel. Laravel is a “clean and classy” modern PHP framework for building web applications. Heavily inspired by the likes of Ruby on Rails, as well as other modern PHP frameworks like Symfony, Laravel 4 aims to show how far PHP has matured over the last couple of years by allowing you to write great web applications from scratch.
Laravel 4 builds upon the hugely successful Laravel 3 framework, but has taken some dramatic steps forward since the last release. Laravel 4 is now a much more testable framework, whilst still maintaining it’s clean and clear syntax as well as adding a wide array of new components and features.
Laravel 4 leverages Composer to manage updates and to decouple the framework into individual components. You are now much more free to mix and match components from the PHP community. Laravel 4 itself uses some of the most popular components from the Symfony framework, amongst others, so that it does not have to reinvent the wheel.
All of these reasons make Laravel 4 the perfect choice of PHP framework for your next project.
Laravel 4 has not been officially released yet, although the final version release is imminent. However, you can still get started with the framework today.
In this tutorial I’m going to show you everything you need to get started with Laravel 4.
This is actually the second time that I have written this tutorial. The first time I was just explaining the process for setting up Laravel 4 that loosely followed the documentation. However, after reading this great tutorial by Chris Fidao, I decided to rewrite my guide but follow the slightly different method provided by Aaron Kuzemchak here. So kudos to those two, you learn something new everyday. The majority of this post will be standing on the shoulder of giants, but hopefully I can expand on some of the nuances in getting started with Laravel 4.
Laravel 4 pretty much needs no configuration out of the box. However, there are a couple of requirements you will need on your local machine.
Firstly you will need to install Composer. Composer is a handy package manager for PHP that allows you to update and manage the different components of your project. If you haven’t already got Composer set up, take a look at my tutorial, What is PHP composer?.
You will also need to be running PHP >= 5.3.7
and you need the MCrypt PHP Extension
to be installed. I will be using PHP 5.4+. I’m using OS X, and so the rest of this tutorial will reference that operating system. I’ll assume that you are already have your environment set up, because it’s outside the scope of this tutorial.
And finally, you will of course need to have Git installed. If you are new to Git, take a look at my overview, Why you need to start using Git today.
So the first thing we need to do is to clone the Laravel repository from GitHub. As I mentioned at the top of this post, Laravel 4 hasn’t officially been released yet, but we can start using it today by cloning the develop
branch.
git clone -o laravel -b develop https://github.com/laravel/laravel.git cribbb
Here I’m using the git clone
command to get the latest copy of the framework and save it into the directory named cribbb
.
-o laravel
means the remote branch will be named laravel instead of the default of origin. This is so we can pull in changes to the framework, but still maintain our own origin
.
-b develop
means we want the develop
branch.
To read more about the git clone
command, take a look at the documentation.
Once the Laravel repo has been cloned, move into the project directory:
cd cribbb
Next we need to create our own Master branch so this project starts at the beginning of it’s own timeline. To do this, run the following command.
git checkout -orphan master
This basically creates a new branch with a new timeline with no parent branches and calls it Master.
Again, to read more about this specific command and the options, take a look at the in-depth documentation.
Now we need to commit the changes.
Run the following command to see all the new framework files that have not been committed to our repo yet:
git status
Next, commit all the files with a message:
git commit -m "Initial commit"
Now when you want to update Laravel, you can run:
git fetch laravel
git fetch
will download updates from the laraval
repository (documentation).
And to merge updates, you would run the following command:
git merge -squash -m "Upgrade Laravel" laravel/develop
This will merge the updates as one commit (documentation).
Next you should run composer install
to pull down all of Laravel’s dependencies.
composer install
Now you can run composer update
to update any composer registered libraries (note I’m running composer.phar
globally from my usr/local/bin
directory).
composer update
Now that you have this set up, whenever a component is updated you can just run composer update
to get the latest updates without having to mess with updating files within directories.
And finally, set your own origin
by running the following command with your own git repo url.
git remote add origin git@github.com:philipbrown/cribbb.git
Now that Laravel 4 has been officially released, the process above can be updated:
First clone the Laravel repo from GitHub:
git clone -o laravel https://github.com/laravel/laravel.git cribbb
Notice how the we don’t need to pass the -b flag to specify the develop
branch.
Move into your new project folder:
cd cribbb
Next we need to rename the master
branch because we want our project to have it’s own master
branch with a clean history. To do that, run the following command:
git branch -move master laravel
This moves the master
to laravel
.
Next we can create a new master
branch with a clean history:
git checkout -orphan master
Commit Laravel to this new project:
git add -A
git commit -m "Initial commit. Add Laravel Framework"
Now to update Laravel, simply run:
git fetch laravel
And then merge the updates:
git merge -squash -m "Upgrade Laravel" laravel/master
The only configuration option that we need to set up now is to create the encryption key that is used within the framework. To do this, all we need to do is to run:
php artisan key:generate
Now that we have Laravel installed, it’s all ready to run. If you are using PHP 5.4, you can run the following command from Terminal to quickly start up the server without having to create a new Virtual Host.
$ php artisan serve
Now if you visit https://localhost:8000 in your browser you should see the Hello World! front page.
If you aren’t using PHP 5.4, you will just need to create a new VirtualHost and point it to the /public
directory of your project.
Now is a good time to commit your work to Git.
$ git add -A
$ commit -m "Yay"
Congratulations! You have just set up Laravel 4!
This is the first of a huge series on creating a web application from scratch. Over the course of many future weeks, I’m going to show you how to create a web application and everything that goes into it.
The application I’m going to create is going to be called Cribbb. I haven’t fully decided what it’s going to be yet, so I’m just letting the project find it’s own direction.
I’m going to be (obviously) using Laravel for all of the server side stuff and we’ll create a RESTful API.
I’m going to use Ember.js for all of the client side stuff and we’ll be using Sass and a lot more cool new front end stuff that you might not have used in a project before.
Hopefully this will be a good guide for going from an idea to a fully working web application as well as practical tutorials in getting started with some of the latest technology for building online products.
And all these tutorials will be free to web and all of the code will be open sourced on GitHub.
So if you have an idea for an online application, but you don’t know where to get started, follow along with this tutorial series as I show you everything you will need to do to create a modern web application.
Remember to follow Culttt on Twitter, Facebook or Google Plus to make sure you never miss a tutorial!
See you next Monday!