Christian Giupponi
Christian Giupponi

Reputation: 7618

Laravel - Add Alias only for local environment

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',

Upvotes: 3

Views: 2570

Answers (2)

A.Alinia
A.Alinia

Reputation: 624

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

Upvotes: 3

Greg
Greg

Reputation: 1853

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.

Upvotes: 13

Related Questions