Last active
June 10, 2016 07:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
App\Providers\LocalServiceProviderProvider
$providers
array with the environment-specific providers you want to registerApp\Providers\LocalServiceProviderProvider
inconfig/app.php