noop's avatar
Level 5

Here to declare aliases on Laravel 11

I have some facades and want to declare aliases for that, usually I use something like this on config/app.php

'aliases' => Facade::defaultAliases()->merge([ 'Setting' => App\Facades\Setting::class, 'HelperMethods' => App\Facades\HelperMethods::class, ])->toArray(),

But in Laravel 11 this aliases is not longer present on app.php, if I add it it works, but I want to know if there are a better and more recommended alternative

0 likes
6 replies
LaryAI's avatar
Level 58

In Laravel 11, the approach to registering facades has been streamlined to encourage the use of fully-qualified class names directly, which is more in line with modern PHP practices. However, if you still want to use aliases for your facades, you can register them in your application's service providers.

Here's how you can do it:

  1. Create a new service provider or use an existing one. If you're creating a new one, you can use the artisan command:
php artisan make:provider AliasServiceProvider
  1. In your service provider, within the register method, you can use the AliasLoader to register your aliases. Here's an example:
<?php

namespace App\Providers;

use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

class AliasServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Get the AliasLoader instance
        $loader = AliasLoader::getInstance();

        // Add your aliases
        $loader->alias('Setting', \App\Facades\Setting::class);
        $loader->alias('HelperMethods', \App\Facades\HelperMethods::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
  1. Finally, you need to register your new service provider. Add it to the providers array in config/app.php:
'providers' => [
    // Other Service Providers

    App\Providers\AliasServiceProvider::class,
],

Now, Laravel will register your aliases when the application boots, and you'll be able to use them just like you did in previous versions of Laravel.

Remember that while aliases can be convenient, they can also lead to less clear code, especially for new developers on your team who may not be familiar with the aliases you've set up. It's generally recommended to use the fully-qualified class names for clarity and better IDE support.

8 likes
davidvandertuijn's avatar

@LaryAI In Laravel 11 you register the service providers in bootstrap/providers.php that return an Array OR in bootstrap/app.php: withProviders()

bigdogdman1's avatar

@davidvandertuijn so there's no way to register them directly inside the return Application::configure line of bootstrap/app.php? Becuase @noop said "if I add it it works"; when you add what exactly, noop? config/app.php? Or the 'aliases' array to app.php?

davidvandertuijn's avatar

@bigdogdman1 i have no idea, but i also have abandoned libraries who depend on the facade aliases. for now i keep them in config/app.php:

'aliases' => Facade::defaultAliases()->merge([
    'Lavacharts' => Khill\Lavacharts\Laravel\LavachartsFacade::class,
])->toArray(),
mabdullahsari's avatar

There's no "better way". That's still the way to go.

But even better: stop using global aliases. They pollute the global namespace.

raveren's avatar

I'd strongly suggest to no longer rely on the no-longer-documented Laravel feature and if you need this functionality just use the PHP native way:

if (! class_exists('Setting')) { // in case the same php process is reused
     class_alias(\App\Facades\Setting::class, 'Setting');
}

This is what Laravel was using under the hood either way.

I personally love this functionality for specific classes - especially for use in Blade files

1 like

Please or to participate in this conversation.