douju1865
2015-04-12 16:44
浏览 75
已采纳

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.

    点赞 评论
  • dongyihao1099 2015-09-25 12:09

    You can add aliases exactly like how you did it for the ServiceProvider. In your Http/Providers/AppServiceProvider update the register method like this:

    public function register()
    {
        if ($this->app->environment() == 'local') {
            // Providers
            $this->app->register('Barryvdh\Debugbar\ServiceProvider');
    
            // Aliases
            $this->app->alias('Debugbar', 'Barryvdh\Debugbar\Facade');
        }
    }
    
    点赞 评论

相关推荐 更多相似问题