Change bundle config on Laravel3

タグ: Laravel3  

Bundles are Laravel extension packages.

Many bundles were made flexible by using config files. It provide easy way to change bundle's behavior.

But if you changed those config files on config folder, you will get a trouble when update the bundle. Because use artisan bundle:update or overwrite bundle folder, your setting will be vanished.

So Taylor, who develop Laravel, recommended to use a event in the official forum. Like that:

Event::listen('laravel.started: melon', function()
{
     Config::set('melon::config.count', '10');
});

If you want to change many items of config, it is better to make a file like application/config/bundles/melon.php, and set key / value of the config file.

return array(
    'count' => '10',
    'color' => 'red',
    'kind' => 'water',
    'size' => 'big',
);

And load in the event.

Event::listen('laravel.started: melon', function()
{
    $config = require_once path('app').'config/bundle/melon.php';
    Config::set('melon::setting', array_merge(Config::get('melon::setting'), $config));
});

It is simple. Of cause you want to do something else in laravel.started envent.

(But... honestly, I think it is easier to do it in global before filter on routes.php. Opps, this is top secret. ;) )