0

config/app.php providers array — order matters

Intermediate5 min read·lv-03-006

Concept

config/app.php is Laravel's central application configuration file. Its providers array controls which service providers load at boot time, and the aliases array registers Facade class aliases.

Provider loading order:

  1. Framework providers (from laravel/framework) run first — they register core services.
  2. Package providers (auto-discovered or manually added) run next.
  3. Application providers (App\Providers\*) run last, allowing them to override framework or package bindings.

Why order matters:

  • If two providers register the same binding (e.g., both bind CacheInterface), the LAST one wins.
  • In boot(), a provider loaded earlier cannot guarantee that a later-loaded provider's binding exists — but since all register() calls happen before any boot(), this isn't usually a problem.
  • Routes from providers are merged — order affects which route matches first if two routes share a path.

The aliases array: Creates PHP class aliases. 'Cache' => \Illuminate\Support\Facades\Cache::class registers a global alias so Cache::get() works without use Illuminate\Support\Facades\Cache. In modern Laravel, these are auto-loaded via Illuminate\Foundation\Bootstrap\RegisterFacades.

config/app.php other important keys:

  • name: Application name (used in notifications, emails).
  • env: Application environment (production, local, testing). Read via app()->environment().
  • debug: Enables detailed error pages. MUST be false in production.
  • url: Application URL. Used for URL generation, link validation.
  • timezone / locale / fallback_locale: Internationalization settings.
  • key: App key used for encryption. Generate: php artisan key:generate.

Code Example

php
<?php
// config/app.php
return [
    'name'     => env('APP_NAME', 'Laravel'),
    'env'      => env('APP_ENV', 'production'),
    'debug'    => (bool) env('APP_DEBUG', false),
    'url'      => env('APP_URL', 'http://localhost'),
    'timezone' => 'UTC',
    'locale'   => 'en',
    'key'      => env('APP_KEY'),
    'cipher'   => 'AES-256-CBC',

    'providers' => [
        /*
         * Laravel Framework Service Providers (order matters within this group)
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers (auto-discovered packages may not appear here)
         */
        // Laravel\Sanctum\SanctumServiceProvider::class, // auto-discovered

        /*
         * Application Service Providers (override framework/package bindings)
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
    ],

    'aliases' => \Illuminate\Support\Facades\Facade::defaultAliases()->merge([
        // Add custom aliases here
        'Cart' => App\Facades\Cart::class,
    ])->toArray(),
];