我需要在Laravel 5应用程序中设置facade
别名,但我希望它仅在我的local environment
中可用。
我尝试添加的存储库是Laravel-Debugbar。
所以我以这种方式在Http/Providers/AppServiceProvider
注册了提供商:
public function register()
{
if( $this->app->environment() == "local")
{
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
}
}
如何为别名执行相同操作?
'Debugbar' => 'Barryvdh\Debugbar\Facade',
答案 0 :(得分:10)
按以下步骤进行。
1)在你的composer require命令中添加“--dev”。即
sudo composer require barryvdh/laravel-ide-helper --dev
或者,在composer.json中,将您的开发环境需要转移到“require-dev”部分:
"require": {
"laravel/framework": "5.0.*"
},
"require-dev": {
"laracasts/generators": "~1.1",
"barryvdh/laravel-ide-helper": "~2.0",
"barryvdh/laravel-debugbar": "~2.0"
},
然后做作曲家更新。
2)设置一个新的ServiceProvider:
php artisan make:provider LocalEnvironmentServiceProvider
3)在config / app中将LocalEnvironmentServiceProvider添加到providers数组:
'providers' => [ ...
'app\Providers\ConfigServiceProvider',
'app\Providers\EventServiceProvider',
'app\Providers\RouteServiceProvider',
/*
* Our Local Environment Service Providers...
*/
'app\Providers\LocalEnvironmentServiceProvider',
],
4)修改新的app / Providers / LocalEnvironmentServiceProvider文件:
<?php namespace App\Providers;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
class LocalEnvironmentServiceProvider extends ServiceProvider {
/**
* List of Local Environment Providers
* @var array
*/
protected $localProviders = [
'Laracasts\Generators\GeneratorsServiceProvider'
'Barryvdh\Debugbar\ServiceProvider',
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
];
/**
* List of only Local Environment Facade Aliases
* @var array
*/
protected $facadeAliases = [
'Debugbar' => 'Barryvdh\Debugbar\Facade',
];
/**
* Bootstrap the application services.
* @return void
*/
public function boot() {
if ($this->app->isLocal()) {
$this->registerServiceProviders();
$this->registerFacadeAliases();
}
}
/**
* Register the application services.
* @return void
*/
public function register() {
}
/**
* Load local service providers
*/
protected function registerServiceProviders() {
foreach ($this->localProviders as $provider) {
$this->app->register($provider);
}
}
/**
* Load additional Aliases
*/
public function registerFacadeAliases() {
$loader = AliasLoader::getInstance();
foreach ($this->facadeAliases as $alias => $facade) {
$loader->alias($alias, $facade);
}
}
}
我还在其中添加了其他几个典型的开发提供商和外观,所以希望您能够轻松了解扩展它的难易程度。
使用此方法,您只需更改$ this-&gt; app-&gt; isLocal()以匹配其他环境。
答案 1 :(得分:2)
您可以添加别名,就像您对ServiceProvider
所做的一样。在Http/Providers/AppServiceProvider
更新register
方法,如下所示:
public function register()
{
if ($this->app->environment() == 'local') {
// Providers
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
// Aliases
$this->app->alias('Debugbar', 'Barryvdh\Debugbar\Facade');
}
}