Getting Started with Laravel 4

With the release of Laravel 4 just around the corner a lot of people are trying to decide whether to start a new project with a stable 3.2 build or wait for the new version. I found myself in that position earlier this week so I decided to pull down Laravel 4 and work off that since I figured the transition would be smoother once a stable version 4 is released.

There isn’t really any documentation on how to get started with Laravel 4, and if you aren’t familiar with Composer, you might not know where to begin. That’s why I’m writing this. If you want to get started with Laravel 4 today, here’s how.

Laravel, Illumniate and Composer Packages

Laravel 4 breaks each core component of the framework into a Composer package. These packages are currently grouped together under the “Illuminate” organization on GitHub. All of these components are used by the Laravel framework, but now that they are stored as separate packages, they can easily be pulled into other projects with Composer.

Note: Familiarity with Composer isn’t necessary for this tutorial, but if you want to learn more, check out Easy Package Management With Composer by Phil Sturgeon.

Since Laravel 4 is dependent on Composer, you already have access to any package from the Packagist directory anytime you start a new project. That means you have thousands of packages at your fingertips and it eliminates the need for proprietary package management systems like Laravel Bundles and CodeIgniter Sparks.

Getting Started

There’s not much to this, it’s just 3 simple steps. I work on a Mac, but I’d imagine these instructions are pretty similar for other operating systems.

1. Pull Down the Application Template

First thing you need to do is pull down the Laravel application template. This is the starting point for Laravel 4 projects. The template is pretty empty, and if you’re coming from Laravel 3, you’ll immediately notice the absence of the laravel, bundles and storage directories.

2. Install Composer

Run the following script to install Composer into the root of your project:

curl -s http://getcomposer.org/installer | php

You should see a success message if everything runs correctly.

Troubleshooting

I received the following error when I tried to install Composer the first time:

Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:

The detect_unicode setting must be disabled.
Add the following to the end of your `php.ini`:
    detect_unicode = Off

If you encounter this error, just do as it says and add detect_unicode = Off to the end of your php.ini file at /private/etc/php.ini.

3. Install the Laravel Components

After composer is installed, you can use it to install all of the other Laravel components.

php composer.phar install

Running this command will install the packages that are specified in the composer.json file. Here’s what that looks like:

{
    "require": {
        "illuminate/foundation": ">=1.0.0"
    },
    "autoload": {
        "classmap": [
            "app/controllers",
            "app/domain",
            "app/tests/TestCase.php"
        ]
    },
    "minimum-stability": "dev"
}

This doesn’t seem like much considering how many packages get installed when you run php composer.phar install, but that’s because each package can have its own dependencies. If we take a look at Illuminate’s foundation package, we can see that it has a whole bunch of its own dependencies that get installed too.

{
    "name": "illuminate/foundation",
    "description": "A web application foundation.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "authors": [
        {
            "name": "Taylor Otwell",
            "email": "taylorotwell@gmail.com"
        }
    ],
    "require": {
        "php": ">=5.3.0",
        "illuminate/auth": ">=1.0.0",
        "illuminate/cache": ">=1.0.0",
        "illuminate/config": ">=1.0.0",
        "illuminate/container": ">=1.0.0",
        "illuminate/cookie": ">=1.0.0",
        "illuminate/database": ">=1.0.0",
        "illuminate/encryption": ">=1.0.0",
        "illuminate/events": ">=1.0.0",
        "illuminate/exception": ">=1.0.0",
        "illuminate/filesystem": ">=1.0.0",
        "illuminate/http": ">=1.0.0",
        "illuminate/pagination": ">=1.0.0",
        "illuminate/profiler": ">=1.0.0",
        "illuminate/redis": ">=1.0.0",
        "illuminate/routing": ">=1.0.0",
        "illuminate/session": ">=1.0.0",
        "illuminate/support": ">=1.0.0",
        "illuminate/translation": ">=1.0.0",
        "illuminate/validation": ">=1.0.0",
        "illuminate/view": ">=1.0.0",
        "symfony/browser-kit": "2.1.0",
        "symfony/css-selector": "2.1.0",
        "symfony/dom-crawler": "2.1.0",
        "symfony/http-kernel": "2.1.0",
        "symfony/translation": "2.1.0",
        "underscore/Underscore.php": "1.3.1"
    },
    "require-dev": {
        "mockery/mockery": "0.7.*"
    },
    "autoload": {
        "psr-0": {
            "IlluminateFoundation": "src/"
        },
        "files": [
            "src/helpers.php"
        ]
    },
    "minimum-stability": "dev"
}

Start Your App

That’s everything! You can go visit your app just like you normally would and developing for Laravel is pretty much the same as it was before. Setup some controllers or start defining some routes!

Let me know if you have any troubles with this and I’ll be sure to revise this article or provide more troubleshooting tips!

  • Repo-man

    About to start a new project and had held off because of Laravel 4. This gives me the chance now to crack on! Thanks!

    • http://chris-schmitz.com/ Chris Schmitz

      No problem – hope this helps you out! Make sure to report back if you have any issues following the steps here!

  • Guest

    this looks rather messy when its all installed. Been wondering – if Symfony already uses composer and a fair few of its own components are used in the latest iteration of Laravel ….why not just use Symfony? Just a thought.

    • http://chris-schmitz.com/ Chris Schmitz

      Yeah, Symfony is obviously a solid choice for a full-stack PHP framework, but Laravel is a lot smaller and easier to wrap your head around. I also think that Laravel has a more expressive syntax, but that’s just a matter of preference.

  • roger_1

    After sussing out a silly permissions issue this worked a dream; until I tried to use artisan. Regardless of what command I try (http://laravel.com/docs/artisan/commands) it’s returning an error:

    [InvalidArgumentException]
    Command “command-name-here” is not defined.

    Any ideas?

    • http://chris-schmitz.com/ Chris Schmitz

      It might be because he has been working on the Artisan CLI the past week or so. I would pull down the latest (`composer update`) and then try this again, I’m guessing it’s fixed by now.

      Sorry it’s taken me so long to get back to you… For some reason I’m not getting email notifications about comments right now.

    • http://spotonsolutions.net Mike

      yeah it seems like artisan is pretty much completely broken currently, but this is pre-release software, so I guess you have to expect some broken things. I’m just excited someone finally put up instructions on how to check it all out so I can play with it.

      • http://chris-schmitz.com/ Chris Schmitz

        I pulled down the latest and it looks like Artisan is setup with the migration commands now if you want to check it out.

      • http://chris-schmitz.com/ Chris Schmitz

        I pulled down the latest and it looks like Artisan is setup with the migration commands now if you want to check it out.

  • Repo-man

    If you get the detect Unicode error when installing Composer. Instead of editing your php.ini you can do this:

    curl -s getcomposer.org/installer | php -d detect_unicode=Off

    http://www.andrew-kirkpatrick.com/2012/10/install-composer-for-php-on-zend-server-mac-os-x/

    • http://chris-schmitz.com/ Chris Schmitz

      Thanks for the tip! It’s also probably best to do a system-wide install of Composer rather than the local install for the project like I suggest in the article… I’ll revise that sometime.

    • http://workfunc.com/ Jonah Bitautas

      Nice!

  • http://www.sinaneldem.com.tr Sinan Eldem

    I have succussfully installed L4. Thanks.

    • http://chris-schmitz.com/ Chris Schmitz

      Sure thing, Sinan :)

  • pablo

    thanks!!

    • http://chris-schmitz.com/ Chris Schmitz

      No problem, hope it helped you out!

    • http://chris-schmitz.com/ Chris Schmitz

      No problem, hope it helped you out!

  • xelam

    on nginx+php-fpm – “500 Internal Server Error” :-(

    • http://chris-schmitz.com/ Chris Schmitz

      What are you getting in your logs? Can you provide any additional details?

    • http://chris-schmitz.com/ Chris Schmitz

      What are you getting in your logs? Can you provide any additional details?

    • http://treahauet.com/ Trea Hauet

      Make sure you have server writable permissions set for the storage directory.

  • Holmes Bryant

    Thank you for this walk-through. It seems the “models” folder has disappeared in L4…

    • http://chris-schmitz.com/ Chris Schmitz

      Yeah, I believe it has been replaced by the “domain” directory. You can put your models in there, but they won’t be registered until you run a composer update after creating them.

  • qrczak

    What is “domain” folder inside “app” folder?

    • http://chris-schmitz.com/ Chris Schmitz

      I believe that is supposed to replace the models directory from Laravel 3. However, you will need to run a composer update after you create your models inside of the directory. I’m not exactly sure what happens there, but it must register them with the framework when you update your composer packages.

    • http://www.facebook.com/basco.johnkevin John Kevin Marbella Basco

      Chris is right. it is just the models folder. But the latest version of laravel 4 in github renamed the domain folder back to models.

  • clauddiu

    Hey, thanks for the updates about Laravel 4. It seems blade engine have some missing functions, anyways, do you guys have a clue how to use Eloquent as stand-alone library?

    • http://chris-schmitz.com/ Chris Schmitz

      Sorry, I’m not sure, but you’ll want to look at Illuminate/Database now for Eloquent updates. Maybe try to dissect how Illuminate/App configures it?

  • maikeldaloo

    Any idea why the vendor directory is being ignored?
    Is that intentional?

    • http://chris-schmitz.com/ Chris Schmitz

      It’s being ignored because that’s where Composer puts everything that it pulls in. With Composer, they want dependencies to be managed separately on each machine rather than syncing all the third-party libs with the application code.

      • maikeldaloo

        Ok, that’s interesting.

        I started a new Laravel 4 project today and on my machine, I ran ‘composer install’ to get all the files then pushed the app to my development server, but the vendor directory wasn’t uploaded so I ran ‘composer install’ on the server as well. Is that the correct way to go about it?

        I use https://github.com/resmo/git-ftp to upload my files, and since vendor wasn’t in the repository, it didn’t get uploaded.

        • http://chris-schmitz.com/ Chris Schmitz

          Yeah, I think that is what you’ll have to do when you deploy your app. You’ll probably want to setup a deployment script, with something like Capistrano, that pushes your files and updates your packages for you. That’s the way you have to deploy with Ruby on Rails and Rubygems, which is similar to how Laravel and Composer work together.

          • maikeldaloo

            Thanks for confirming.
            Yeah, it might be time to get back to using Capistrano.

  • Gabriel Braila

    Laravel will continue to be a simple framework or will demand a more educated developer that eventually will end up working with Symfony (could be a very good thing…) ?

  • http://www.facebook.com/basco.johnkevin John Kevin Marbella Basco

    Thank you very much for this one :) I’m gonna try it.

  • Ronnie Visser

    For soem reason the /vendor/autoload.php file isn’t there. so I am not able to start off..

    • http://chris-schmitz.com/ Chris Schmitz

      Hmm… Did everything install correctly with Composer? I just tried setting up a new app and everything was there. Maybe Taylor was in the middle of an update. Right now I think the dependencies are linked to the latest commits in their respective repos, so he might have committed something since then that changed this. Maybe just give it another try.

  • http://creolab.hr Boris Strahija

    Really liking the new version :) Does anyone know if there’s any support for some kind of modules?
    In L3 I was using bundles to organize my controllers, models, etc. Since L4 doesn’t have the same type of bundles as far as I can see, but composer packages, is there some sort of option for modules?

    • http://chris-schmitz.com/ Chris Schmitz

      Yeah, Composer replaces bundles in Laravel 4. Obviously you aren’t going to be creating Composer packages for completely proprietary code though, so I think you need to use the more traditional MVC organization for you files rather than the HMVC-esque organization that bundles gave us in Laravel 3.

      Here’s some more information from Taylor about bundles in Laravel 4: http://www.thenerdary.net/post/33888660150/bundles-in-laravel-4

      • http://creolab.hr Boris Strahija

        I’ve seen that article. Well routes can now be bound to any class, so I could actually create a modules folder, and only setup the autoload paths, and everything should work I guess. Anyway it would be nice to have something like that built in by default ;)

        • http://chris-schmitz.com/ Chris Schmitz

          Agreed! I’m sure we’re not the only ones thinking that, so I’m sure a convention will form quickly or maybe there will be support for something like that eventually.

  • Ian Simmons

    Thank you Chris. I pick up a lot more from Taylor’s L4 videos when I can actually install it and work along with them :-)

    • http://chris-schmitz.com/ Chris Schmitz

      Haha, yeah, it helps to be able to actually get up and running! Glad this helped you out!

      • Ian Simmons

        Hey Chris is there any documentation available yet? I’m trying to generate an application key and the command doesn’t seem to be available with artisan ‘key:generate’ like with L3. I’m picking up on other commands with Taylors videos and Jeffrey Way’s ‘connected to the backbone’ series but this seems to be the last piece I need for the initial setup. Thanks

        • http://chris-schmitz.com/ Chris Schmitz

          Yeah, I don’t think key:generate has made it into the console package yet, but I’m assuming it will be brought over eventually. For now you’ll need to generate your own and just copy it into your config if you want to use sessions.

          • Ian Simmons

            Ok, thanks Chris. :-) Yeah I found where the commands are registered in illuminate/console/src/start.php . Not much there but it’s enough to get started

    • http://chris-schmitz.com/ Chris Schmitz

      Haha, yeah, it helps to be able to actually get up and running! Glad this helped you out!

      • Ian Simmons

        Hey Chris is there any documentation available yet? I’m trying to generate an application key and the command doesn’t seem to be available with artisan ‘key:generate’ like with L3. I’m picking up on other commands with Taylors videos and Jeffrey Way’s ‘connected to the backbone’ series but this seems to be the last piece I need for the initial setup. Thanks

  • http://www.facebook.com/drfraker Dustin Fraker

    Awesome, just what I was looking for. Thanks for the article!

    • http://chris-schmitz.com/ Chris Schmitz

      Sure thing, Dustin! Glad to hear this helped you out.

  • Stephen

    When I run “php composer.phar install” at step 3, I always get this error.

    [UnexpectedValueException]
    ‘E:developmentprojectsapp_dirvendor/swiftmailer/swiftmailer/891ae9991dbb2bc3c16dbefda60638c7.1′ is not a zip archive.

    I understand this is a composer.phar error but I cannot get my way around it.

    • Stephen

      I will be very happy if someone can help me on this.

    • http://chris-schmitz.com/ Chris Schmitz

      I’m sorry, I’m not sure what would be causing this… I would try posting this, along with some additional information about the error and your environment, to stackoverflow.com.

      • http://twitter.com/nielsenbryan Bryan Nielsen

        Great instructions! If anyone would like to keep the install contained in a virtual machine I’ve been doing some work on this vagrant setup for Laravel 4 – https://github.com/bryannielsen/Laravel4-Vagrant

        • http://chris-schmitz.com/ Chris Schmitz

          Awesome, thanks for sharing, Bryan!

    • Stephen

      It worked. I added “–prefer-source” to step 3. By the way, my environment is Windows 7.
      Thanks.

      • http://chris-schmitz.com/ Chris Schmitz

        Glad to hear you got up and running. Thanks for sharing how you ended up getting it to work!

  • Stephen

    When I run “php composer.phar install” at step 3, I always get this error.

    [UnexpectedValueException]
    ‘E:developmentprojectsapp_dirvendor/swiftmailer/swiftmailer/891ae9991dbb2bc3c16dbefda60638c7.1′ is not a zip archive.

    I understand this is a composer.phar error but I cannot get my way around it.

    • http://chris-schmitz.com/ Chris Schmitz

      I’m sorry, I’m not sure what would be causing this… I would try posting this, along with some additional information about the error and your environment, to stackoverflow.com.

  • http://twitter.com/nielsenbryan Bryan Nielsen

    Great instructions! If anyone would like to keep the install contained in a virtual machine I’ve been doing some work on this vagrant setup for Laravel 4 – https://github.com/bryannielsen/Laravel4-Vagrant

  • http://workfunc.com/ Jonah Bitautas

    Thanks Chris, this was super helpful.

  • Jeremy Schaffer

    During the ‘php composer.phar install’ process I am getting some text that state several suggestions for installation. Is this something I need to worry about?

    symfony/translation suggests installing symfony/config (2.1.*)
    symfony/translation suggests installing symfony/yaml (2.1.*)
    symfony/routing suggests installing doctrine/common (>=2.2,<2.4-dev)
    symfony/routing suggests installing symfony/config (2.1.*)
    symfony/routing suggests installing symfony/yaml (2.1.*)
    symfony/event-dispatcher suggests installing symfony/dependency-injection (2.1.*)
    symfony/http-kernel suggests installing symfony/class-loader (2.1.*)
    symfony/http-kernel suggests installing symfony/config (2.1.*)
    symfony/http-kernel suggests installing symfony/dependency-injection (2.1.*)
    symfony/http-kernel suggests installing symfony/finder (2.1.*)
    monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
    monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
    monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)

    • http://chris-schmitz.com/ Chris Schmitz

      Sorry, just catching up on this now… Are you still getting this error? I’m assuming it’s resolved by now with the beta release of Laravel, but let me know if you are still having issues!

      • http://galley.net jimg3

        Hi Chris… still getting the error (Feb3) from composer install on laravel develop branch.

        symfony/translation suggests installing symfony/config (2.2.*)
        symfony/translation suggests installing symfony/yaml (2.2.*)
        symfony/routing suggests installing symfony/config (2.2.*)
        symfony/routing suggests installing symfony/yaml (2.2.*)
        symfony/routing suggests installing doctrine/common (~2.2)
        symfony/event-dispatcher suggests installing symfony/dependency-injection (2.2.*)
        symfony/http-kernel suggests installing symfony/class-loader (2.2.*)
        symfony/http-kernel suggests installing symfony/config (2.2.*)
        symfony/http-kernel suggests installing symfony/dependency-injection (2.2.*)
        monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
        monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
        monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
        monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
        monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)

        • http://chris-schmitz.com/ Chris Schmitz

          Hmm, I’m not sure… Laravel does have its own documentation and installation instructions for Laravel 4 now so it might help to check that out: http://four.laravel.com/

          • http://galley.net jimg3

            Thanks anyway – same notices when pulling from latest.zip .. but it’s a beta release so one has to expect this sort of thing.

        • http://chris-schmitz.com/ Chris Schmitz

          Hmm, I’m not sure… Laravel does have its own documentation and installation instructions for Laravel 4 now so it might help to check that out: http://four.laravel.com/

          • http://galley.net jimg3

            Thanks anyway – same notices when pulling from latest.zip .. but it’s a beta release so one has to expect this sort of thing.

  • Alexis Mejias

    How can I go about installing L4 if the application template mentioned here has been removed?

    • http://chris-schmitz.com/ Chris Schmitz

      There is actually a release candidate for Laravel 4 in the develop branch of the main Laravel repository on GitHub. I believe you can just pull that down and install it the same way: https://github.com/laravel/laravel/tree/develop

      • Alexis Mejias

        Yeah I realized that after while :P Thanks tho

    • http://chris-schmitz.com/ Chris Schmitz

      There is actually a release candidate for Laravel 4 in the develop branch of the main Laravel repository on GitHub. I believe you can just pull that down and install it the same way: https://github.com/laravel/laravel/tree/develop

      • Alexis Mejias

        Yeah I realized that after while :P Thanks tho

  • vipin

    I am working on 4.1 version of laravel then how can i do for resizing of image

  • vipin

    I am working on 4.1 version of laravel then how can i do for resizing of image

  • vipin

    I am working on 4.1 version of laravel then how can i do for resizing of image