Custom Facades in Laravel

Facades are static interface of a class to encapsulate its default arguments.

First we need to define a class in app directory

like app/CustomClass/NewClass.php

namespace App\CustomClass;

class NewClass
    public function __construct(variable1)
        $this->variable1 = $variable1; 
    public function yourMethod(Request $request)
        return "something";

Now we need to define a facade for this class

Firstly we need to edit AppServiceProvider.php

In app/Providers/AppServiceProvider.php

Use class defined in app directory

use App\CustomClass\NewClass;

Add following to boot method

$this->app->singleton("NewClassFacade", function(){
    return new NewClass('in');

insert NewClassFacade.php in app directory


namespace App;

class NewClassFacade
    protected static function resolveFacade($name)
        return app()[$name];
    public static function __callStatic($method, $arguments)
        return (self::resolveFacade('NewClassFacade'))

To call the facade:

Just add the facade to your controller

use App\NewClassFacade;

And call facade like any static method

$output = NewClassFacade::yourMethod();

Leave a Reply

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