Facade Laravel

  • 04-10-2022
  • 1016
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Facade in laravel provides a static class interface to the application. There are many facade classes already inbuilt when you create a new laravel application with the composer framework. The facade is a global class that allows you to call directly any of the files in the application by giving a facade file.

Laravel facade provided a global namespace to the class. To create a manual facade in the app we need to create a facade class and need to register it in the configuration. The facade we will register in the service provider and the service provider we will register in the app.php config file.

Creating a facade in the application is very easy to add the application with various modifications because it's a traditional static method for a flexible terse benefit.

Create a Facade Laravel Step :

  • Step 1. Create Service Provider.
  • Step 2. Define Facade Methods.
  • Step 3. Define Facade.
  • Step 4. Register Facade.
  • Step 5. Register Provider.
  • Step 6. Define Route For Test.
  • Step 7. Run & Test.

So let's start creating a facade in the laravel application. it's a normal process of creating.

Step 1. Create Service Provider

Create a service provider using the below-provided command in your app terminal. You can add the service provider name as you wish.

php artisan make:provider DemoFacadesServiceProvider

Step 2. Define Facade Methods

Create a file inside the App\Test\DemoFacades.php, the Test folder you will create yourself. after creating a class, register the method which you want to use with the facade. We can also pass the argument inside the facade method. You can number of facade methods according to your wants.

App\Test\DemoFacades.php
<?php

namespace App\Test;

class DemoFacades {

     public function testingFacades()
     {
          echo "Testing the Facades in Laravel.";
     }

     public function get($val) 
     {
          return $val * 10;
     }
}

Step 3. Define Facade

Define a facade inside the App\Test\Facades, Create a folder inside the Test folder. After it adds a DemoFacades.php file with the class inside the facade folder.

Follow the given below code sample in your application. import facade and extend the class to the facade which you created.

App\Test\Facades\DemoFacades.php
<?php

namespace App\Test\Facades;

use Illuminate\Support\Facades\Facade;

class DemoFacades extends Facade {

     protected static function getFacadeAccessor() { 
          
          return 'demo'; 
     }
}

Step 4. Register Facade

Register created facade inside the service provider, The service provider which we created earlier uses the command. you will see in the below example register method. We will bind all the created methods in the facade.

App\Providers\DemoFacadesServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App;

class DemoFacadesServiceProvider extends ServiceProvider
{
     /**
     * Register services.
     *
     * @return void
     */
     public function register()
     {
          App::bind('demo',function() {
               return new \App\Test\DemoFacades;
          });
     }

     /**
     * Bootstrap services.
     *
     * @return void
     */
     public function boot()
     {
          //
     }
}

Step 5. Register Provider

Register created service provider in the app.php file. In this file, there is a providers attribute array add service provider like the below example.

config/app.php
'providers' => [
     /*
     * Laravel Framework Service Providers...
     */
     .....
     App\Providers\DemoFacadesServiceProvider::class,
],

Give globally namespace to the facade by adding in aliases attribute.

'aliases' => [
     .....
     'DemoFacades' => App\Test\Facades\DemoFacades::class,
],

Step 6. Define Route For Test

Create a route for testing purposes. You do not need to do it if you are working on a real project, apply where you want to.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

use App\Test\Facades\DemoFacades;

Route::get('facade-test-1', function() {

     return DemoFacades::testingFacades();
});

Route::get('facade-test-2', function() {

     return DemoFacades::get(123);
});

Step 7. Run & Test

Start the development server with the provided command in your application.

php artisan serve

Copy below the given URL and paste it into your browser and test your created facade. If you are working on your real project then not need to do it, use it according to your usage.

http://127.0.0.1:8000/facade-test-1

http://127.0.0.1:8000/facade-test-2

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :