普通网友 2015-04-12 16:44
浏览 77
已采纳

Laravel - 仅为本地环境添加别名

I need to set a facade alias in my Laravel 5 application but I want that it is available only in my local environment.

The repository I'm trying to add is Laravel-Debugbar.

So I registered the Providers in Http/Providers/AppServiceProvider in this way:

public function register()
{
    if( $this->app->environment() == "local")
    {
        $this->app->register('Barryvdh\Debugbar\ServiceProvider');
    }
}

How can I do the same for the alias?

'Debugbar' => 'Barryvdh\Debugbar\Facade',
  • 写回答

2条回答 默认 最新

  • douchun2158 2015-04-17 02:07
    关注

    proceed as follows.

    1) Add "--dev" to your composer require commands. ie

     sudo composer require barryvdh/laravel-ide-helper --dev
    

    Alternatively, in composer.json move your development environment requires into a "require-dev" section:

    "require": {
        "laravel/framework": "5.0.*"
    },
    "require-dev": {
        "laracasts/generators": "~1.1",
        "barryvdh/laravel-ide-helper": "~2.0",
        "barryvdh/laravel-debugbar": "~2.0"
    },
    

    then do a composer update.

    2) Set up a new ServiceProvider:

    php artisan make:provider LocalEnvironmentServiceProvider
    

    3) in config/app add the LocalEnvironmentServiceProvider to the providers array:

    'providers' => [ ...
        'app\Providers\ConfigServiceProvider',
        'app\Providers\EventServiceProvider',
        'app\Providers\RouteServiceProvider',
    
       /*
        * Our Local Environment Service Providers...
        */
        'app\Providers\LocalEnvironmentServiceProvider',
    ],
    

    4) Modify your new app/Providers/LocalEnvironmentServiceProvider file:

    <?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);
            }
        }
    }
    

    I added a couple of other typical development providers and facades in there as well, so hopefully you get the gist of how easy it is to extend this.

    Of course with this method you can simply change $this->app->isLocal() to match other environments too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下