Skip to content

require-dev in Laravel

THIS POST IS NOW OBSOLETE. This post referred to Laravel 4, and the information was always a little iffy. This post remains only to prevent this blog from being entirely empty.


 

Note: this post, like all Laravel tips, is best read in Jeffrey Way’s voice.

There are some packages (Jeffrey Way’s Generators and Barry van den Heuvel’s IDE Helper come to mind) which we only need during development, but which become tightly coupled with Laravel. Once you’ve added the packages to the array of ServiceProviders in app/config/app.php, you have to either have a different config for development or include those packages in your deployment. Deploying them isn’t going to do any harm, but I think it’s a good habit to keep your deployment as lean as possible — the fewer packages on your production server, the fewer things that can go wrong.

One way to keep these packages from having to be deployed is to have a different config file for your local machine. Laravel certainly supports this, but I’m not wild about it as a solution to this particular problem. There’s (presently) no way to merge an array of service providers in the main config/app.php file with one in config/local/app.php. You’d have to maintain two lists and make sure that you keep them in sync. Add a service provider in one file, and it’s up to you to remember to add it in the other.

EDIT:Matt Stauffer points out that Laravel already has a built-in helper function for this: append_config. Check out the Laravel docs for more detail.

[And now we return to the original, obsolete post]

I’ve come up with a different solution that I’m using — I don’t promise that it will work for you, but I don’t see why it wouldn’t.

Instead of adding service providers to the list in app/config/app.php, I manually register them from /app/start/local.php. That way, they’re only available during local development. Here’s an example with Jeffrey Way’s Laravel 4 Generators (which you should totally install.)

First, we add the package to our composer.json file as a development dependency:

"require-dev": { 
   "way/generators": "2.*" 
},

We run composer update --dev so that composer knows we want the development packages too.

The instructions for installing Generators tells us to add 'Way\Generators\GeneratorsServiceProvider' to the service providers array. That’s just what we won’t be doing. Instead, we’re going to edit /app/start/local.php and … add one line. Really, that’s all it takes:

<?php     $app->register( new \Way\Generators\GeneratorsServiceProvider($app));

Assuming that your environment correctly determines that it’s local (/bootstrap/start.php is your friend and so is Steve Grunwell’s article Laravel Application Environments without Hostnames), you’ll have php artisan generate commands available on your development machine without having to muck around when it’s time to deploy.

Published inLaravel

5 Comments

  1. Isn’t it better to list it in the local app configuration file? /app/config/local/app.php

  2. Jake Wilson Jake Wilson

    I just wanted to point out that arrays in the config files already append to each other by default. For example,

    Take the default Laravel 4.2.x installation:

    dd( Config::get('app.providers') );

    prints out:

    array (size=26)
    0 => string 'Illuminate\Foundation\Providers\ArtisanServiceProvider' (length=54)
    1 => string 'Illuminate\Auth\AuthServiceProvider' (length=35)
    2 => string 'Illuminate\Cache\CacheServiceProvider' (length=37)
    3 => string 'Illuminate\Session\CommandsServiceProvider' (length=42)
    4 => string 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider' (length=61)
    5 => string 'Illuminate\Routing\ControllerServiceProvider' (length=44)
    6 => string 'Illuminate\Cookie\CookieServiceProvider' (length=39)
    7 => string 'Illuminate\Database\DatabaseServiceProvider' (length=43)
    8 => string 'Illuminate\Encryption\EncryptionServiceProvider' (length=47)
    9 => string 'Illuminate\Filesystem\FilesystemServiceProvider' (length=47)
    10 => string 'Illuminate\Hashing\HashServiceProvider' (length=38)
    11 => string 'Illuminate\Html\HtmlServiceProvider' (length=35)
    12 => string 'Illuminate\Log\LogServiceProvider' (length=33)
    13 => string 'Illuminate\Mail\MailServiceProvider' (length=35)
    14 => string 'Illuminate\Database\MigrationServiceProvider' (length=44)
    15 => string 'Illuminate\Pagination\PaginationServiceProvider' (length=47)
    16 => string 'Illuminate\Queue\QueueServiceProvider' (length=37)
    17 => string 'Illuminate\Redis\RedisServiceProvider' (length=37)
    18 => string 'Illuminate\Remote\RemoteServiceProvider' (length=39)
    19 => string 'Illuminate\Auth\Reminders\ReminderServiceProvider' (length=49)
    20 => string 'Illuminate\Database\SeedServiceProvider' (length=39)
    21 => string 'Illuminate\Session\SessionServiceProvider' (length=41)
    22 => string 'Illuminate\Translation\TranslationServiceProvider' (length=49)
    23 => string 'Illuminate\Validation\ValidationServiceProvider' (length=47)
    24 => string 'Illuminate\View\ViewServiceProvider' (length=35)
    25 => string 'Illuminate\Workbench\WorkbenchServiceProvider' (length=45)

    Now, create a local/dev environment app.php, install some package, like the php debug bar, and add this to the local/dev app.php:


    array(
    'Barryvdh\Debugbar\ServiceProvider',
    ),
    );

    Note, the providers array only has ONE value in it. Now go back to your view and refresh:


    array (size=26)
    0 => string 'Barryvdh\Debugbar\ServiceProvider' (length=33)
    1 => string 'Illuminate\Auth\AuthServiceProvider' (length=35)
    2 => string 'Illuminate\Cache\CacheServiceProvider' (length=37)
    3 => string 'Illuminate\Session\CommandsServiceProvider' (length=42)
    4 => string 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider' (length=61)
    5 => string 'Illuminate\Routing\ControllerServiceProvider' (length=44)
    6 => string 'Illuminate\Cookie\CookieServiceProvider' (length=39)
    7 => string 'Illuminate\Database\DatabaseServiceProvider' (length=43)
    8 => string 'Illuminate\Encryption\EncryptionServiceProvider' (length=47)
    9 => string 'Illuminate\Filesystem\FilesystemServiceProvider' (length=47)
    10 => string 'Illuminate\Hashing\HashServiceProvider' (length=38)
    11 => string 'Illuminate\Html\HtmlServiceProvider' (length=35)
    12 => string 'Illuminate\Log\LogServiceProvider' (length=33)
    13 => string 'Illuminate\Mail\MailServiceProvider' (length=35)
    14 => string 'Illuminate\Database\MigrationServiceProvider' (length=44)
    15 => string 'Illuminate\Pagination\PaginationServiceProvider' (length=47)
    16 => string 'Illuminate\Queue\QueueServiceProvider' (length=37)
    17 => string 'Illuminate\Redis\RedisServiceProvider' (length=37)
    18 => string 'Illuminate\Remote\RemoteServiceProvider' (length=39)
    19 => string 'Illuminate\Auth\Reminders\ReminderServiceProvider' (length=49)
    20 => string 'Illuminate\Database\SeedServiceProvider' (length=39)
    21 => string 'Illuminate\Session\SessionServiceProvider' (length=41)
    22 => string 'Illuminate\Translation\TranslationServiceProvider' (length=49)
    23 => string 'Illuminate\Validation\ValidationServiceProvider' (length=47)
    24 => string 'Illuminate\View\ViewServiceProvider' (length=35)
    25 => string 'Illuminate\Workbench\WorkbenchServiceProvider' (length=45)

    See that first entry? The debug bar. The production providers array has been automatically appended to whatever you have in your local/dev providers array.

    There is no need to use the config_append with arrays, unless you are trying to do something in runtime.

Leave a Reply to Jake Wilson Cancel reply

Your email address will not be published. Required fields are marked *