Let’s say you like to use Laravel 4 Generators by Jeffery Way like we do at Clicksco. If you are not familiar with this package then you should head over to Laracasts and watch the free video. You will only be using the generators in development so you don’t really want to have this included on production. Let’s have a look at how we can do this.

First of all you need to make sure that you have included ‘way/generators’ in the ‘require-dev’ section of your composer.json file and not the ‘require’ section. You can do this manually or from terminal by running

php composer require --dev "way/generators:2.*"

Worth noting that if you run this then composer will automatically run a ‘composer update’ and this update will run including require-dev packages, this is the default action of a composer update. Your composer.json file will now include the package in the ‘require-dev’ section.

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

After requiring the package, which now sits in your vendor folder, you will need to instruct Laravel to use it. To do this we register a service provider. If you were to follow the installation instructions on GitHub then you will notice that you are asked to open ‘app/config/app.php’, and add a new item to the providers array (shown below).

... 'providers' => array( 'WayGeneratorsGeneratorsServiceProvider', ), ...

This is where we are going to start doing things a little differently.

Instead of doing the above we are actually going to add the service provider to our development environment app config.

As I am sure you are aware, inside the ‘app/config’ folder you can create folders that match your environments. By default the ‘app/config/app.php file will be loaded by Laravel and this will be true for production. However, for development we want to tell Laravel to load ‘app/config/development/app.php’. To do this we need to alter ‘bootstrap/start.php’ and include something like:

<?php ... $env = $app->detectEnvironment([ 'development' => [ 'Jamess-MacBook-Pro.local', 'precise32', 'localhost.dev', 'localhost', ], 'production' => [ 'live.mydomain.com' ] ]); ...

The above example has my local machine hostname for situations where I would be using MAMP and also the hostname of my Vagrant VM for when I am working in our default Vagrant setup we have at Clicksco. I then have the domain of the live application in the production array. There are a number of different ways of managing this but I have found this to work best for my needs.

Another way, and one that is much better

<?php ... $env = $app->detectEnvironment(function () { return isset($_SERVER['ENV']) ? $_SERVER['ENV'] : 'development'; }); ...

We have told Laravel to check in the ‘app/config/development’ folder for settings to be used when in our development environment we now need to add our service provider to this file.

Normally we would just add the config settings for development in a new array inside the ‘app/config/development/app.php’ file but by default this will override the settings loaded from ‘app/config/app.php’. So we are going to make use of the ‘append_config’ helper method in our environment app configuration file, see below.

<?php return array( 'debug' => true, 'providers' => append_config( array( 'WayGeneratorsGeneratorsServiceProvider', ) ), );

Notice that the debug = true is an example of an override and our providers = append_config is an example of us appending to what has already been loaded. Now we have access to Jeffery Ways Generators but only when we are in development, you can test this by running

php artisan

and you will now see something like

[text]

generate

generate:controller Generate a controller

generate:migration Generate a new migration

generate:model Generate a model

generate:pivot Generate a pivot table

generate:publish-templates Copy generator templates for user modification

generate:resource Generate a new resource

generate:scaffold Scaffold a new resource (with boilerplate)

generate:seed Generate a database table seeder

generate:view Generate a view

[/text]

To finish off we will want to make sure that we don’t include the ‘require-dev’ packages on production. This is simply done by running composer update adding the –no-dev option. Just add the ‘–no-dev’ option to your deployment script.

composer update --no-dev

Hope this helps someone.

Share this: Tweet







WhatsApp

Email

