How to Create Custom Facade in Laravel 11 / 10

How to Create Custom Facade in Laravel 11 / 10

To create custom facade class in laravel; Simply create php class file and bind the class with provider, after that create custom facade helper class and register it in config\app.php as aliases and use it.

Note that, The Facade pattern is a software design pattern that is often used in object-oriented programming. A facade is a class wrapping a complex library to provide a simpler and more readable interface to it.

How to Create Custom Facade in Laravel 11 / 10

Steps to create, configure and use a custom facade class in laravel 11/10/9 apps:

Step 1 – Create a PHP class file

In this step, you need to create a PHP helper class Tutsmake.php in App\Tutsmake. You can create a folder of your own choice instead of Tutsmake.

namespace App\Tutsmake;

class Tutsmake
{
    public function sayHello()
    {
        echo "Hello, from Facade class.";
    }
}

Step 2 – Bind that class to Service Provider

In this step, you need to open your terminal and execute the following command on it:

php artisan make:provider TutsmakeServiceProvider

Bind this class to a Service Provider. Then add below code in register method for binding our class:

$this->app->bind('tutsmake',function(){

        return new Tutsmake();

});

Now, your service provider class will look like below:

namespace App\Providers;

use App\Tutsmake;

use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

class TutsmakeServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('tutsmake',function(){

        return new Tutsmake();

        });
    }
}

Step 3 – Register that Service Provider in Config\app.php

In this step, you need to register that service provider in config\app.php as providers:

/*
         * Application Service Providers...
         */
        App\Providers\TutsmakeServiceProvider::class,

Step 4 – Create a class that extends Illuminate\Support\Facades\Facade

In this step, you need to create a file TutsmakeFacade in App\Tutsmake which will extend Illuminate\Support\Facades\Facade. in our example, class will look like below.

namespace App\Tutsmake;

use Illuminate\Support\Facades\Facade;

class TutsmakeFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'tutsmake';
    }
}

Step 5 – Register your facade in Config\app.php as aliases

In this step, you need to register class created in Step 4 in config\app.php as aliases:

'Tutsmake'   =>  App\Tutsmake\TutsmakeFacade::class

Test This Facade

Let’s test your facade by adding a simple route which will resolve to a closure function implementing our facade class.

Route::get('/tutsmake', function() {

    Tutsmake::sayHello();

});

Visit your browser with the above route and you will see the hello message.

Conclusion

Laravel create custom facade example. In this tutorial, you have learned how to create and use custom facade in laravel 6, 7, 8, 9,10 apps.

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *