DEV Community

Cover image for Define your own facade in laravel
Mohammad Reza
Mohammad Reza

Posted on • Edited on

7 2

Define your own facade in laravel

In this article we want to know how can we define our own facade
and use it in our application

In this article I want to make SmartLogger class and try to use it with Facades so lets start

Create a directory in app and which has SmartLogger name and fill it like below

.
├── SmartLoggerFacade.php
└── SmartLogger.php

0 directories, 2 files
Enter fullscreen mode Exit fullscreen mode

SmartLogger.php

<?php

namespace App\SmartLogger;

class SmartLogger {
    public function log($text) {
        $path = storage_path('logs');
        $myfile = fopen("{$path}/SmartLog.txt", "a");
        fwrite($myfile, $text);
        fwrite($myfile, PHP_EOL);
        fclose($myfile);
    }
}
Enter fullscreen mode Exit fullscreen mode

SmartLoggerFacade.php

<?php

namespace App\SmartLogger;
use Illuminate\Support\Facades\Facade;

class SmartLoggerFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'smartlogger';
    }
}
Enter fullscreen mode Exit fullscreen mode

lets make service provider

php artisan make:provider SmartLoggerServiceProvider
Enter fullscreen mode Exit fullscreen mode

SmartLoggerServiceProvider.php

<?php

namespace App\Providers;

use App\SmartLogger\SmartLogger;
use Illuminate\Support\ServiceProvider;

class SmartLoggerServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('smartlogger',function(){
            return new SmartLogger();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
Enter fullscreen mode Exit fullscreen mode

Now open config\app.php and add this lines in it

Add it in "providers" array

...
        App\Providers\SmartLoggerServiceProvider::class
Enter fullscreen mode Exit fullscreen mode

Add it in "aliases" array

...
        'Smartlogger'   =>  App\SmartLogger\SmartLoggerFacade::class
Enter fullscreen mode Exit fullscreen mode

Now lets test it :)

Add this code in your controller

SmartLogger::log("Hi");
Enter fullscreen mode Exit fullscreen mode

And add this in top of your controller

use App\SmartLogger\SmartLoggerFacade as SmartLogger; 
Enter fullscreen mode Exit fullscreen mode

Feel free to ask any questions :)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay