Laravel Facades: The Complete Guide

Laravel ships with so many facades provide access to almost all of Laravel’s features. 

use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    return Cache::get('key');
});

Laravel Facades

Laravel Facades provide a “static” interface to classes available in the application’s service container. Laravel facade serves as “static proxies” to underlying classes in the service container. All of Laravel’s facades are defined in the Illuminate\Support\Facades namespace.

First, download the Fresh Laravel. Then, in the app directory, make one folder called Person. In that folder, create one file called Person.php.

<?php

// Person.php

namespace App\Person;

class Person
{
    public function getName()
    {
        return 'AppDividend';
    }
}

We need to include this method in the routes  >>  web.php file to call this method.

<?php

// web.php

use App\Person\Person;

Route::get('/', function () {
    $person = new Person();
    echo $person->getName();
});

So, here in this example, the Person.php file has one non-static method. So, we have made one object of it and then called the method, and we can see the output in the browser.

Now, change the code like this in the web.php file.

<?php

// web.php

Route::get('/', function () {
    Person::getName();
});

Run this in the browser, and you will see this.

Class ‘Person’ not found.”

So, if you still want the output result like “AppDividend,” you must use the Power of Facades.

Facades

In the app directory, create one more folder called, Facades, and in that folder, make one file called Person.php.

<?php

namespace App\Facades;

class Person extends \Illuminate\Support\Facades\Facade
{
    public static function getFacadeAccessor()
    {
        return 'name';
    }
}

We need to map this accessor to our original Person\Person.php class and make its object. That is where the service container is required. We can, though, write this logic in web.php or create one file called NameServiceProvider. 

For this example, we will write this logic in the web.php file.

<?php

// web.php

app()->bind('name', function() {
    return new \App\Person\Person;
});

Route::get('/', function () {
    return Person::getName();
});

Here, we bind the facade accessor to the original class on which we need to create an object. So behind the scenes, we are newing up the instance and calling its method.

Also, we need to add aliases in the config  >> app.php file.

// app.php

'aliases' => [
   'Person' => App\Facades\Person::class,
]

Now, again hit the following URL: http://localhost:8000/

You will see “AppDividend” as an output. So we have achieved the goal of calling that method, which looks static, but behind the scenes, we are creating the object and calling the method with that object.

Laravel provides almost every service with the help of the Facade pattern. That makes it unique from the other PHP Framework.

That’s it for this tutorial.

3 thoughts on “Laravel Facades: The Complete Guide”

  1. shouldn’t it be use AppFacadesPerson; instead of use AppPersonPerson; in web.php ? Otherwise it shows error. I am using Laravel 5.7

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.