sven/env-providers

Load Laravel service providers based on your application's environment.

v4.2.0 2024-10-23 13:37 UTC

This package is auto-updated.

Last update: 2024-11-23 13:47:41 UTC


README

env-providers

Laravel EnvProviders

Latest Version on Packagist Total Downloads Software License Build Status StyleCI

A more finetuned way of managing your service providers in Laravel. This package allows you to configure the environment certain service providers and aliases are loaded in.

Installation

Via composer:

$ composer require sven/env-providers

Or add the package to your dependencies in composer.json and run composer update to download the package:

{
    "require": {
        "sven/env-providers": "^4.0"
    }
}

Next, add the ServiceProvider to your providers array in config/app.php:

// config/app.php
'providers' => [
    ...
    Sven\EnvProviders\ServiceProvider::class,
];

Usage

You must publish this package's configuration file for it to work properly. To do so, run the following command:

$ php artisan vendor:publish --provider="Sven\EnvProviders\ServiceProvider"

After that, you should see the file config/providers.php. In the created configuration file you can see 2 pre-defined provider groups that will help you set up what providers and aliases should be loaded when the application is in any of the configured environments.

Environments

In the environments array you can define what are known as "environment aliases". For example, if you use more than one name for local development (eg. dev, development, and local), you can alias all of these to one name to use in this package's configuration.

Note: You can set your application's environment in either config/app.php under env or via your .env file. If you want to manage your .env file via php artisan, you can check out sven/flex-env.

Groups

The groups key in the configration is used to load in service providers and aliases (also know as facades) in one of the previously defined environments. You can use * as a wildcard here to always load that group, regardless of the application's environment.

Providers

The providers array is where you can put the providers you want to have loaded in the defined environment. This should be pretty straight forward as it is similar to how you would register service providers in config/app.php.

Aliases

In the aliases array you may define all your aliases (facades). As with the providers, this is the same as how you would register aliases in the default config/app.php configuration file.

Example

return [
    'environments' => [
        'dev' => ['local', 'development', 'dev'],
        'prod' => ['production'],
    ],
    
    'groups' => [
        'dev' => [
            'providers' => [
                Sven\ArtisanView\ArtisanViewServiceProvider::class,
                Barryvdh\Debugbar\ServiceProvider::class,
            ],
            'aliases' => [
                'Debugbar' => Barryvdh\Debugbar\Facade::class,
            ],
        ],
        'prod' => [
            'providers' => [ /* ... */ ],
            'aliases' => [ /* ... */ ],
        ],
        '*' => [
            'providers' => [ /* ... */ ],
            'aliases' => [ /* ... */ ],
        ],
    ],
    
],

Notice how we're only loading the Debugbar ServiceProvider and facade when our application's environment is either local, development, or dev. This means we can't use the Debugbar facade in our project when the environment doesn't match any of those.

Contributing

All contributions (pull requests, issues and feature requests) are welcome. Make sure to read through the CONTRIBUTING.md first, though. See the contributors page for all contributors.

License

sven/env-providers is licensed under the MIT License (MIT). Please see the license file for more information.