What is Facades in Laravel and How it works?


Hello Artisan, Hope you guys are doing well. Laravel ships with plenty of useful feature, and one of them is Facades. Today in this article I will let you know what actually Facades is, how it works and what is alternative of it.

From the official Definition of Laravel,

Facades provide a “static” interface to classes that are available in the application’s service container.

Laravel facades serve as “static proxies” to underlying classes in the service container.

Laravel ships with many facades which provide access to almost all of Laravel’s features. Facades examples include ViewRouteResponse, ConfigAuth and many more. Check list of Facades given by Laravel in below screenshot.

 

Now lets understand Facades in Detail,

Facades in simple words is just something that provides a static interface to methods of classes that are binded in the service container, a Facades does this using a magic method. You can check this article to know more about Service Container.

When To Use Facades?

  1. Facades provide a shortmemorable syntax that allows you to use Laravel’s features without remembering long class names that must be injected or configured manually.
  2. Since facades are so easy to use and do not require injection, it can be easy to let your classes continue to grow and use many facades in a single class.

How Facades works in Laravel?

  • Every service inside the Laravel container has a unique name, to access a service directly from the container, we can use the App::make() method or the app() helper function.
  • Take a look at below Example,
App::make('some_service')->methodName();
  • As Facades provides Static interface you can use directly it with help of scope resolution operator. Check below Syntax,
someService::methodName();
==> Here "someService" is Facade Class and  "methodName" is Static method of that Facade.

 

Every Facade class extends Base Facade class which is located at Illuminate/Support Package. and Facade class engage with only one method which is getFacadeAccessor(), Which returns the name of service. Check below image for more understanding,

Based on the key returned by this function Laravel finds appropriate service from the service container.

Click Here to see the list of the class which is being used by particular Facade.

You can create your custom Facade and bind custom logic for that Facade. You just have to register this custom Facade in aliases array of config/app.php file.

Now let’s understand how Base Facade class are being called and how it works.

The Base Facade class has an implementation of the magic function __callStatic().

This magic method helps us to fetch service from the container, see below syntax of it,

public static function __callStatic($name, $args){
    return app()->make(static::getFacadeAccessor())->$name();
}

 

How Laravel handles the all Facades?

Laravel handles all this Facades using Aliases, All alias names are stored in an aliases array inside the app.php config file, which is located inside the config directory of your project.

When Laravel app is bootstrapped, it iterates through all the aliases array element with the help of AliasLoader, and creates a queue of __autoload functions and each __autoload() function is responsible for creating an alias for the respective Facade class.

Due to this functionality from Laravel, we won’t have to import and alias the classes before using them, We can directly access them.

Alternative of Facades

What if we don’t want to call method on Facades class, then alternative is Helper functions

To complement Facades Laravel offers a variety of global “helper functions” that make it even easier to interact with common Laravel features Let’s understand it using example,

For example, instead of using Illuminate\Support\Facades\Response Facade to generate a JSON response, we may simply use the response() helper function

Because helper functions are globally available, you do not need to import any classes in order to use them, check below code snippet to understand Facades and helper functions,

use Illuminate\Support\Facades\Response;
Route::get('/users', function () {
    return Response::json([
        // Here we have used Response Facade
    ]);
});
Route::get('/users', function () {
     return response()->json([
        // Here we have used response() helper
     ]);
});

 

There is absolutely no practical difference between Facades and Helper functions.



Share this post