Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

Aman Jain, December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container.

Let’s start with step by step

Step 1 : Create a class

For a better a structure and understandable code we are going to create a folder facade in app folder and then we create a file CustomFacade.php

<?php
namespace App\Facades;
class DateHelper
{
    public function hello()
    {
        echo "Hello, I am Readerstacks";
    }
}

Step 2 : Create a service provider

Now to register our new facade we need to register it in Laravel service container so run below command to generate the service provider

php artisan make:provider DateHelperServiceProvider

and then add bind our facade in register function

<?php

namespace App\Providers;

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

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

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

Here we called bind method which accepts name and anonymous function to resolve the dependency.

Step 3 : Register the new service provider

Service Providers not come into the effect until it’s register to config/app.php providers array thus let’s register the our new service provider as below

 'providers' => [
   ......
   ......
        App\Providers\DateHelperServiceProvider::class

    ],

Step 4 : Create a facade class

We have created a class, service provider and registered it to app.php now we need to create a facade to access the method of DateHelper class statically so create a facade as below in same location app\Facades\DateHelperFacade.php and it extends class Illuminate\Support\Facades\Facade . we need to create getFacadeAccessor method which return the name of facade same as we passed in bind method in service provider.

<?php
namespace App\Facades;


use Illuminate\Support\Facades\Facade;
class DateHelperFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'datehelper'; // same as bind method in service provider
    }
}

Now we will be able to access our new facade as below

<?php

use Illuminate\Support\Facades\Route;
 

Route::get('/facade',function(){
     return App\Facades\DateHelperFacade::hello();
});

What if we wanted to access it using alias then we need to register it in aliases.

Step 5 : Create a alias

So, if you wanted to access without full namespace of the class then we nee to define it in config/app.php aliases array as below

'aliases' => [

         ........
        'Date' => App\Facades\DateHelperFacade::class
        
    ],

Now check our implementation by creating a route

<?php

use Illuminate\Support\Facades\Route;
 

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

    return Date::hello();

}); 
Screenshot 2021 12 22 at 8.19.59 AM
Laravel Custom Facade example

Share this:

  • Facebook
  • X

Related

Php Laravel facadelaravelphp

Post navigation

Previous post
Next post

Related Posts

Php How to Send Mail in Laravel Through Gmail SMTP

How to Send Mail in Laravel Through Gmail SMTP ?

May 9, 2022May 10, 2022

You can easily configure Send Mail in Laravel Through Gmail SMTP by enabling the app password and less secure apps in your settings. Email is very common operation of any website like sending an email to users after registration, Send newsletters to users and many more. In this tutorial i…

Share this:

  • Facebook
  • X
Read More
Devops How to install PHP

How to install PHP on Ubuntu?

September 18, 2021October 4, 2022

PHP is widely used langauge in world of web. PHP is an open sourece language and free for everyone. It’s a server side scripting lanaguge which compiles on server and can embedded with html too. Currently, there is three version are available of PHP that are as below PHP 5…

Share this:

  • Facebook
  • X
Read More
Php Laravel 9 CRUD with Search, Image and Pagination

Laravel 9 CRUD with Search Image upload and Pagination

June 25, 2022November 7, 2023

In this article i will learn laravel 9 CRUD with Search Image upload and Pagination. In this post we will not only cover the CRUD operation but also the validation on form, unique validation, image uploading and view the uploaded image, Flash messages, Search the uploaded data and many more….

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version