Skip to content

Instantly share code, notes, and snippets.

@manuelcoppotelli
Last active June 10, 2016 07:40
Show Gist options
  • Save manuelcoppotelli/7d3907d5095d5ef2c080 to your computer and use it in GitHub Desktop.
Save manuelcoppotelli/7d3907d5095d5ef2c080 to your computer and use it in GitHub Desktop.
Allow to register some Service Providers only for a specific environment in Laravel 5.1
<?php
namespace Larabook\Providers;
use Illuminate\Support\ServiceProvider;
class LocalServiceProviderProvider extends ServiceProvider
{
/**
* The Service Providers to register only for the specific environment.
*
* @var array
*/
protected $providers = [
'Laracasts\Generators\GeneratorsServiceProvider',
];
/**
* The environment you want to register the services for.
*
* @var string
*/
protected $env = 'local';
/**
* Register the application services.
*
* @return void
*/
public function register()
{
if ($this->app->environment($this->env) && ! empty($this->providers)) {
$this->bindLocalProviders();
}
}
/**
* Loop through the stored providers and register each of them with the IoC container.
*
* @return void
*/
private function bindLocalProviders()
{
foreach ($this->providers as $provider) {
$this->app->register($provider);
}
}
}
@manuelcoppotelli
Copy link
Author

Instructions

  1. Download this gist in App\Providers\LocalServiceProviderProvider
  2. Fill the $providers array with the environment-specific providers you want to register
  3. Add App\Providers\LocalServiceProviderProvider in config/app.php
  4. Enjoy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment