普通网友 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条)

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。