Getting started with Laravel 4

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.

Requirements

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.

Installing Laravel 4

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  
 [/bash]

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](http://git-scm.com/docs/git-clone).

Once the Laravel repo has been cloned, move into the project directory:  
 ```bash  
 cd cribbb  
 [/bash]

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.  
 ```bash  
 git checkout –orphan master  
 [/bash]

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](http://git-scm.com/docs/git-checkout).

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:  
 ```bash  
 git status  
 [/bash]

Next, commit all the files with a message:  
 ```bash  
 git commit -m "Initial commit"  
 [/bash]

Now when you want to update Laravel, you can run:  
 ```bash  
 git fetch laravel  
 [/bash]

`git fetch` will download updates from the `laraval` repository ([documentation](http://git-scm.com/docs/git-fetch)).

And to merge updates, you would run the following command:  
 ```bash  
 git merge –squash -m "Upgrade Laravel" laravel/develop  
 [/bash]  
 This will merge the updates as one commit ([documentation](http://git-scm.com/docs/git-merge)).

Next you should run `composer install` to pull down all of Laravel’s dependencies.  
 ```bash  
 composer install  
 [/bash]

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).  
 ```bash  
 composer update  
 [/bash]

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.  
 ```bash  
 git remote add origin git@github.com:philipbrown/cribbb.git  
 [/bash]


## *Update for Laravel 4 Master*

Now that Laravel 4 has been officially released, the process above can be updated:

First clone the Laravel repo from GitHub:  
 ```bash  
 git clone -o laravel https://github.com/laravel/laravel.git cribbb  
 [/bash]

Notice how the we don’t need to pass the -b flag to specify the `develop` branch.

Move into your new project folder  
 ```bash  
 cd cribbb  
 [/bash]

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:  
 ```bash  
 git branch –move master laravel  
 [/bash]  
 This moves the `master` to `laravel`.

Next we can create a new `master` branch with a clean history:  
 ```bash  
 git checkout –orphan master  
 [/bash]

Commit Laravel to this new project:  
 ```bash  
 git add -A  
 git commit -m "Initial commit. Add Laravel Framework"  
 [/bash]

Now to update Laravel, simply run:  
 ```bash  
 git fetch laravel  
 [/bash]

And then merge the updates  
 ```bash  
 git merge –squash -m "Upgrade Laravel" laravel/master  
 [/bash]


## Configuring Laravel

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:  
 ```bash  
 php artisan key:generate  
 [/bash]


## Running Laravel

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.  
 ```bash  
 $ php artisan serve  
 [/bash]

Now if you visit [http://localhost:8000](http://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.  
 ```bash  
 $ git add -A  
 $ commit -m "Yay"  
 [/bash]

Congratulations! You have just set up Laravel 4!


## About this series

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](http://cribbb.com). 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](http://culttt.com/2013/04/15/what-are-restful-web-services/).

I’m going to use [Ember.js](http://emberjs.com/) for all of the client side stuff and we’ll be using [Sass](http://culttt.com/2013/01/14/getting-started-with-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](https://github.com/philipbrown/cribbb).

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](http://twitter.com/culttt), [Facebook](http://facebook.com/culttt) or [Google Plus](https://plus.google.com/105545788844824874492/posts) to make sure you never miss a tutorial!

See you next Monday!

To view a full listing of the tutorials in this series, [click here](http://culttt.com/tag/cribbb/).