Adds some features on the top of laravel facades

v1.0.10 2023-02-15 15:00 UTC

This package is auto-updated.

Last update: 2023-09-15 16:11:57 UTC


Strategy pattern in laravel, made easy

by adding some features on top of laravel facades.

Built with ❤️ for every smart laravel developer

                                           Quality Score Code Quality Build Status Software License StyleCI

🔦 Installation:

composer require imanghafoori/laravel-smart-facades

⚡️ No need to have getFacadeAccessor()


use Illuminate\Support\Facades\Facade;

class MyFacade extends Facade
    protected static function getFacadeAccessor() // <--- normal facade
        return 'some_key'; 


use Imanghafoori\SmartFacades\Facade;

class MyFacade extends Facade
    //                                          <--- smart facade

⚡️ Setting the default driver by shouldProxyTo($class):

Instead of bind a string to a concrete class with IOC container, you can choose the low level implementation class like this:

public function register()    // <-- in a service provider
    if ($someCondition) {
        MyFacade::shouldProxyTo( SomeDriver::class );
    } else {
        MyFacade::shouldProxyTo( SomeOtherDriver::class );

You can proxyTo any abstract string (or closure) bound on the IoC container.

Note: If you invoke it twice, it will override:

MyFacade::shouldProxyTo( DriverClass1::class );
MyFacade::shouldProxyTo( DriverClass2::class ); // <--- This wins!

⚡️ Using Non-default Driver:

If you want to change the driver at call site:


⚡️ Method Hooks:

You can introduce some code "Before" and "after" a method call, remotely: (like event listeners on eloquent models)


Here we have told the system evenever the MyFacade::findUser($id) method was called in the system, to perform a log.

⚡️ Choosing the driver, based on parameters value:

For example, lets say you want your facade to use an SMS based driver by default, but if the text is very long (more than 200 chars) it should use an email driver.

You can do it like this:


🔧 Automatic method injection when calling a method through a facade.

This the adds ability to enjoy automatic method injection when calling methods on POPOs (Plain Old Php Objects) WITHOUT any performance hit when you do not need it.

🐙 Example:

class Foo { ... }

class Bar
    // This has dependencies: "Foo", "LoggerInterface"
    public function m1 (Foo $foo, LoggerInterface $logger, string $msg)

Calling Bar through a Facade:


MyFacade::m1(resolve(Foo::class), resolve(LoggerInterface::class), 'hey there !'); 


 // This will work and $foo, $logger would be auto-injected for us.

MyFacade::m1('hey there !');          // normal facade

// or you may want to provide some dependencies yourself:
\Facades\Bar::m1(new Foo('hey man!'), 'hey there !');   //Now only the Logger is injected

🙋 Contributing:

If you find an issue or have a better way to do something, feel free to open an issue or a pull request.

⭐ Your Stars Make Us Do More ⭐

As always if you found this package useful and you want to encourage us to maintain and work on it. Just press the star button to declare your willingness.

More from the author:

Laravel Microscope

💎 Automatically find bugs before they bite.

Laravel Widgetize

💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.

It's not I am smarter or something, I just stay with the problems longer.

"Albert Einstein"