[Proposal] Add Ruby on Rails style initializer files
I was working on a package, and realized that I may require the user to do more than just add a configuration file. I need them to run some code before using my package, and it seemed like the only good way to do this was to tell them to put some code in global.php or routes.php. The normal way of using a configuration file just doesn't fit what I need to do. This doesn't seem like a great solution, and it'd be great to have some standardization much like configurations.
These would be standard php files that go in the directory config/initializers. Once the app loads, Laravel would auto-require them by default which in turn would run the code within them. This should have the same affect as adding code to the bottom of something like global.php, but with a bit more standardization and organization. We could also allow packages to have initializers much like they have configurations and views, and add some commands to copy over initializers from packages for user modifications.
I may be overthinking this and this may not even be useful, so that's why I'm looking for some feedback. Would anyone out there have a use for this feature? I know you can currently add it yourself, but if enough people already roll their own way to do this maybe it'd be useful to standardize.
References: [Ruby on Rails initializers)(http://guides.rubyonrails.org/configuring.html#using-initializer-files)
Can't you use your package service provider boot() method to load/run the code?
If I know what the code is going to be upfront. But I was thinking more along these lines, specifically a package I'm working on to create menus.
I want to provide a pretty detailed API to create nested menus in an application. Currently a configuration doesn't really seem like the best way to do it so here's what I had in mind:
// This would be in user's config/initializers/menu_system/php file.
MenuSystem::create('test', function(&$menu) {
$menu->item('Articles', 'google.ca', function(&$articles) {
$articles->item('First Article', 'facebook.com', function($first) {
$first->item('First Thing', 'first.com');
});
$articles->item('Second Article', 'facebook.com');
});
$menu->item('Twitter', 'twitter.com');
});
Then later, anywhere they want, they could do the following to render the menu:
render_menu('test');
I basically look at it as a way to do more complicated configuration.
Or you could create your directory/file anywhere you want, add to your composer.json > autoload > classmap or files the route to that directory/file and then run php artisan dump-autoload. That's basically what you're proposing.
Yes, I am proposing that. But I'm moreso mentioning making it a standard so that packages can more easily take advantage of features like this without having to give users a bunch of instructions. So basically the plugin provides a template for this initializer file, the user can use a commany like php artisan package install to copy over the initializer.
Ryan, the way I do these sorts of things is I create an artisan command in the package that does all the specialized functionality, then I simply tell the user to run that single artisan command after installing the package instead of giving them "a bunch of instructions", i.e. php artisan menusystem:install, which would - in your case - copy over the initializer, etc.