[easyreview title=”Complexity rating” icon=”geek” cat1title=”Level of experience required, to follow this how-to.” cat1detail=”A good level of familiarity with web hosting will come in handy here, especially if your hosting environment is different from mine. It will also help if you’re comfortable at the command line.” cat1rating=”4″ overall=”false”]
Laravel Tutorials
- Part 1: Installation
- Part 2: Orientation
- Part 3: Using External Libraries
- Part 4: Database Management Through Migrations
- Part 5: Using Databases
- Part 6: Virtualised development environment – Laravel Homestead
Introduction
If, like me, you spend much time coding for the web – for pleasure or profit – sooner or later you’re going to find that you benefit from using a development framework. A framework is a collection of scripts that help you create an application much more quickly. Frameworks typically include a lot of the “nuts and bolts” components – scripts that assist with database connections for example, plus components that impose some structure on your programming.
For a long time, my framework of choice was CodeIgniter. CodeIgniter has stagnated of late and concerns have arisen over licensing. Partly as a consequence, many PHP developers like me have searched for an alternative. In the search, I came across a framework that many programmers are turning to: Laravel. My early experiences with Laravel have been extremely positive and I have found many things I prefer about it. In this series of tutorials, I’ll show you how to get up and running with Laravel and begin creating an application. The application will involve some web-scraping, so you may wish to stay tuned for that reason alone.
Before we dive in though, one word of caution: Laravel is a young open source project. Like many such projects, its documentation is less complete than you might wish, particularly when compared to CodeIgniter. In fact CodeIgniter’s great documentation was one of the reasons why I initially chose it as a development framework. Documentation is a core commitment of the Laravel team, but at the time of writing, with the recent release of Laravel 4, I’m finding the documentation is not quite up to scratch. Possibly you’ve found that too, which is why you’ve made it here to this tutorial. In fact one of the worst parts of the documentation at the time of writing is the installation procedure! With that caveat in place, let’s move on – it’s still well worthwhile.
Prerequisites
All my web coding is done within a Linux environment, usually CentOS or Ubuntu Server. For the easiest experience following these tutorials, you may wish to create a similar environment. (I’ve written about that elsewhere.) Alternatively, you should be able to follow the tutorials with some tweaking – but you’re on your own there. At the very least, I recommend you have in place:
- Apache web server
- Shell access to the server (preferably SSH)
- Root access to install Composer globally (not essential)
- Git must be installed in your environment.
Installing Composer
With the latest release (4) Laravel has taken a leap forward in several areas. One such area is the management of third party libraries and packages. Laravel previously made use of an external project called “Composer“, to install dependent packages. With Laravel 4, you now use Composer to install Laravel itself. To install Composer, from a root login shell, do the following:
cd /usr/local/bin
curl -sS https://getcomposer.org/installer | php
mv composer.phar composer
Provided /usr/local/bin
is in your $PATH environment variable, you will now be able to call Composer with “composer [options] command [arguments]
“.
Installing Laravel
The beauty of Composer is the simplicity it brings to library/application installation. There is an overwhelming range of tutorials on how to install Laravel with Composer. Please bear in mind that many of these were written while Laravel 4 was in beta or even alpha. Now it has been released, the installation is quite straightforward. Having prepared a new environment for a web site (a virtual host or whatever), navigate to the directory above the default web root directory. Then install using Composer. Eg:
cd /home/geek/domains/test.geekanddummy.com/
composer create-project laravel/laravel
You should see a fair bit of output indicating that Composer is creating a directory “laravel
” and pulling in all the dependencies for a basic installation. The laravel
directory contains a folder entitled “public
“, intended to be your web root. Your easiest way to complete the configuration is to point your web site at that directory. For example, using Virtualmin, you would go to Server Configuration –> Website Options and change “Website documents sub-directory” from “public_html” to “laravel/public”.
Having done that, when I browse to my test web site, I see:
Installing other frameworks
Now would be a good time put to put Twitter’s Bootstrap and jQuery in place, if you’re planning to use them. Naturally, we’ll use Composer for this. You might use other frameworks in your web applications – check out Packagist to see if anyone has made a Composer package available.
Composer demands a tutorial all of its own, but I’ll keep it simple here. I’m going to make my new Laravel application depend on the latest compatible branches of Bootstrap and jQuery. This will potentially allow us to upgrade these two frameworks with a simple Composer command at a later date.
In the root of your Laravel application you’ll find the main Composer configuration file, composer.json
. You don’t need to get your hands dirty editing the file, just from a shell in that root directory, issue the following commands:
composer require "components/jquery":"*"
composer require "twitter/bootstrap":"*"
This updates the composer.json
file to include these dependencies and goes ahead and downloads them. It can take a while – be patient.
You’ll end up with jQuery files under ./components/jquery
and Bootstrap files under ./vendor/twitter/bootstrap
. These are locations not visible to your web server (the root is at ./public
, you’ll recall). This is a particular problem in the case of Bootstrap. For now, here’s a quick-and-dirty way of accessing these files. I’m on the lookout for a more elegant solution, but this will get you up and running rapidly. Navigate to the “public
” folder in a login shell and issue the following commands:
mkdir -p assets/css
mkdir assets/img
mkdir assets/js
ln -s ../../../vendor/twitter/bootstrap/img/glyphicons-halflings.png ./assets/img
ln -s ../../../vendor/twitter/bootstrap/docs/assets/css/bootstrap.css ./assets/css
ln -s ../../../vendor/twitter/bootstrap/docs/assets/css/bootstrap-responsive.css ./assets/css
ln -s ../../../components/jquery/jquery.min.js ./assets/js
And so on, for whichever bits you’ll use. This will only work if your web server allows following symbolic links. The .htaccess directive “Options +SymLinksIfOwnerMatch” may help here, but that’s outside the scope of this tutorial.
Configure your development environment
I use NetBeans for development. If you don’t already have a preferred IDE (integrated development environment), I recommend you check it out. Another favourite is Eclipse. You could use an ordinary text editor, but then you’d be missing out on a lot of things that can make your coding more comfortable and efficient.
Having installed Laravel and the other frameworks on my web server, next I use NetBeans to pull the code across to my development environment. In the NetBeans “New Project” wizard, select the option “PHP Application from Remote Server”. In the remote configuration, ensure that you choose as your “upload directory”, the laravel
. From there, you’ll want to download the app
and public
directories.
Conclusion
That’s it for today’s tutorial. Next time, we’ll look at orientating ourself within the framework (“what goes where?”).
Your ugly site makes it hard to read your tutorials, I love to read more laravel stuff, but seriously, red text, dark grey backgrounds, text highlighting of black on dark grey. Seriously this makes me want to stab myself in the face
Noooo! Jeremiah, don’t do it! We will make some changes which will hopefully improve legibility. Watch this space. Well, not literally – we wouldn’t want your facial injuries on our consciences.
All the best,
Geek
what is your mean of “nuts and bolts” components?
By “nuts and bolts” I’m thinking of all the stuff that typically you’d have used in multiple applications. Libraries for querying databases, functions for sending email, MVC routing, classes for generating forms and validating them, that kind of thing.
thanks for that brov
Our pleasure. 🙂
Have the intervening months led to a more elegant solution for the Bootstrap files?
Dave,
I’ve been out of the Laravel game for a while. Just getting back in. I think I’ve drawn the conclusion (per another comment below) that your easiest approach, if you’re not compiling a customised Bootstrap, is to include Bootstrap from a Content Delivery Network, in your main template. (Yeah, I need to do a template tutorial…) The recommended CDN is MaxCDN. Do something like this:
Hope this helps and sorry for the delay.
Geek
Another option is to just not install bootstrap at all and link to Bootstrap CDN, as per the instructions on getbootstrap.com/getting-started – if you’re happy to use someone else’s bandwidth. 🙂
Excellent point, thanks!
amazing tutorial!
Thanks!
Geek Please i need your favor to setup Laravel + composer also i have alread a application . my coder is died i think he is not online anymore . i will pay for it 🙂 . also thanks in advance if you can give some time to restore my application and setup itr properly thanks waiting for responce . you can add me on skype : ug.viet (Philippe Nicolitch)
I’m sorry – this isn’t a service we provide at the moment. Hope you were able to sort it out.