Search code examples
phplaravellaravel-5laravel-5.6facade

How to create facade for singleton in Laravel?


I want to create a facade for a singleton that I'v defined in ServiceProvider:

$this->app->singleton('\ActivityLogger\ActivityLoggerInterface', '\ActivityLogger\ActivityLogger');

How can I have something like this in my code:

Logger::log($data)

thanks


Solution

  • Since version 5.5 Laravel makes it possible to use real-time facades: https://laravel.com/docs/5.5/facades#real-time-facades.

    In order to make it work, you need to prepend the use statement that imports your class with Facades\:

    <?php
    use Facades\ActivityLogger\ActivityLogger as Logger;
    
    //...and then in your code
    Logger::debug($data);