Saturday, November 16, 2024
Google search engine
HomeLanguagesLaravel Create Custom Facade Class Example Tutorial

Laravel Create Custom Facade Class Example Tutorial

Laravel create custom facade example. In this tutorial, you will learn how to create and use custom facade in laravel 6, 7, 8, 9 app.

This tutorial will guide you each thing step by step on how to create and use custom facade in laravel app.

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

Laravel Create Custom Facade Class Example Tutorial

Follow the following steps and create and use custom facade class in laravel app:

  • Step 1 – Create a PHP class file
  • Step 2 – Bind that class to Service Provider
  • Step 3 – Register that Service Provider in Config\app.php
  • Step 4 – Create a class that extends Illuminate\Support\Facades\Facade
  • Step 5 – Register your facade in Config\app.php as aliases

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('neveropen',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('neveropen',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 'neveropen';
    }
}

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('/neveropen', 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 apps.

Recommended:-Laravel Try Catch

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments