What is Facades used in Laravel?
Asked Answered
C

3

21

I'm confused by the Facades offered by Laravel.

The Laravel documentation states:

Facades provide a "static" interface to classes that are available in the application's service container. Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

Please help me to understand:

  1. Why we really use use Illuminate\Support\Facades?
  2. How to create custom Facades ?
Commonage answered 14/2, 2017 at 10:41 Comment(0)
C
25

Props to SitePoint for sharing such informative and helpful knowledge about facades in Laravel.

The facade pattern is a software design pattern that is often used in object-oriented programming.

A facade is a class wrapping a complex library to provide a simpler and more readable interface to it.

Facade pattern

Facades in Laravel

Facades provide a "static" interface to classes that are available in the application's service container. Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

How Facades Are implemented in Laravel

Every service inside the container has a unique name. In a Laravel application, to access a service directly from the container, we can use the App::make() method or the app() helper function.

<?php

App::make('some_service')->methodName();

In Laravel, all services have a facade class. These facade classes extend the base Facade class which is part of the Illuminate/Support package. The only thing that they need to implement is the getFacadeAccessor method, which returns the service name inside the container.

Commonage answered 15/2, 2017 at 7:5 Comment(0)
L
8

In general terms, a Facade (pronounced as /fəˈsɑːd/) is the exterior and front facing side of a building or any thing. Importance of facades are they are easy to notice and more prominent, similarly there is a concept of facades in laravel too. Which are used to manage our code readability and build easy to remember syntax of functions and classes through it.

A Laravel facade is a class which provides a static-like interface to services inside the service container. They serve as a proxy for accessing the underlying implementation of the laravel’s services. For instance write below code in web.php file

//using redis cache
Route::get('/cache', function () {
    cache()->put('hello','world', 600);
    dd(cache()->get('hello')); //outputs world
});

Above example is using non static way to call cache’s method now let’s see how we do using cache facade.

use Illuminate\Support\Facades\Cache;
//using redis cache
Route::get('/cache', function () {
    Cache::put('hello','world', 600);
    dd(Cache::get('hello'));
});

Don’t you think the above example is more elegant and have easy to remember syntax, right ? That’s the beauty of facades.

Lightly answered 27/7, 2019 at 5:49 Comment(2)
in the first example, does it create two different cache instances?Dikmen
@JiechaoWang No we access same cache instance using different ways available.Lightly
C
4

You can understand by this example

DB::table('table_name')->get();  

In this example, the DB is the facade. it is calling the table() static method on the DB facade.

Cadelle answered 26/7, 2018 at 7:27 Comment(3)
I know DB is a facade. But what is a facade?Unsuccess
A facade in simple words is just something that provides a static interface to methods of classes that are binded in the service container, a facade does this using a magic method. To understand facades first understand what service containers are.Downright
In a real-world example, a Facade could be the appearance of a building that you see from outside( nice windows, acrylic tiles, doors). However, the real building is not just that, it's a complex structure that consists of various things. It's the same thing in software engineering, that you don't see the complex code inside, but just beautiful memorable methods does the job.Trappist

© 2022 - 2024 — McMap. All rights reserved.