Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kossa's avatar
Level 20

How to add custom facade in laravel 11?

Hello,

In laravel 10 and previous, I used this code in app.php:

'aliases' => Facade::defaultAliases()->merge([
        'Helpers' => Helpers::class,
    ])->toArray(),

But for laravel 11 I dont know

0 likes
6 replies
LaryAI's avatar
Level 58

In Laravel 11, the process of adding custom facades is similar to previous versions, but you might need to adapt your approach slightly due to changes in the framework's structure or configuration. Here's how you can add a custom facade in Laravel 11:

  1. Create your helper class if you haven't already. For example, let's say you have a Helpers class located in the app/Helpers directory:
namespace App\Helpers;

class Helpers
{
    public static function doSomething()
    {
        // Your custom logic here
    }
}
  1. Create a service provider for your helper class. You can generate a new provider using the Artisan command:
php artisan make:provider HelperServiceProvider
  1. In your newly created HelperServiceProvider, you'll need to register your class and create the facade alias. Open the HelperServiceProvider.php file and add the following code:
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Helpers\Helpers;

class HelperServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('helpers', function ($app) {
            return new Helpers();
        });
    }

    public function boot()
    {
        // If you want to use the facade as 'Helpers' you can register it here
        \Illuminate\Support\Facades\Facade::class_alias('helpers', 'Helpers');
    }
}
  1. Next, you need to register your HelperServiceProvider in the config/app.php file. Add it to the providers array:
'providers' => [
    // Other service providers...

    App\Providers\HelperServiceProvider::class,
],
  1. Finally, you can create a facade for your helper class. Create a new file in the app/Facades directory, for example, HelpersFacade.php, and add the following code:
namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class HelpersFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'helpers'; // This should match the alias you registered in your service provider
    }
}
  1. Now, you can use your facade anywhere in your application like so:
use App\Facades\HelpersFacade as Helpers;

Helpers::doSomething();

Remember to replace Helpers with the actual name of your helper class and facade. This should set up your custom facade in Laravel 11. If there are any changes in the framework that affect this process, be sure to check the Laravel documentation for the latest best practices.

1 like
sreeharshrajan's avatar

@LaryAI This doesnt seem to be working, I am getting error as

Facade does not implement getFacadeAccessor method.

JussiMannisto's avatar

@sreeharshrajan Lary is an AI and doesn't respond. It also hallucinates things occasionally.

The official docs are more reliable than AI or forum responses. But here's all you need to do:

  1. Create a facade class.
  2. Create the underlying class.
  3. Bind the underlying class to the service container. The name of the binding must match the value returned by getFacadeAccessor() in the facade.

Example:

// 1. The facade class:
class Foo extends Facade {
	
	protected static function getFacadeAccessor(): string {
		return 'foo';
	}
	
}

// 2. The underlying class:
class Bar {
	
	public function fizz() {
		dump('buzz');
	}
	
}

// 3. Binding in the register method of AppServiceProvider:
$this->app->singleton('foo', function($app) {
	return new Bar();
});

Then you can use the facade:

Foo::fizz();
1 like
obuidulpias's avatar

@LaryAI I faced this type of error. "Facade does not implement getFacadeAccessor method. " please give this solution.

puklipo's avatar

Forget about short Facade aliases. Always use the full namespace.

use App\Facades\Helpers;

$foo = Helpers::bar();

config/app.php aliases is just an alias. you don't need to use it.

kossa's avatar
Level 20

@puklipo I dont want to import it each time, for that I created a dynamic facade

Please or to participate in this conversation.