AppDividend
Latest Web Development Tutorials

Laravel 5.5 Facades Tutorial

1,007

Laravel 5.5 Facades Tutorial is today’s leading topic. Facades provide a “static” interface to classes that are available in the application’s service container. Laravel ships with so many facades which provide access to almost all of the Laravel’s features. 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.

use Illuminate\Support\Facades\Cache;

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

Laravel 5.5 Facades

Now, we will take one example to illustrate the Laravel Facade. First, download the Fresh Laravel. In 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';
    }
}




To call this method, we just need to include this file in the routes  >>  web.php file.

<?php

// web.php

use App\Person\Person;

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

So, here in this example, Person.php file has one non-static method. So, we have made one object of it and then call 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, you will see like this.

Class ‘Person’ not found.”

Related Posts
1 of 12

So, if you still want the output result like “AppDividend,” then you have to 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';
    }
}

Now, we need to map this accessor to our original Person\Person.php class and make its object. That is where service container is required. We can though write this logic in web.php, or we can 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 are binding 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 call 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 the “AppDividend” as an output. So we have achieved the goal of calling that method, which looks like static but behind the scenes, we are creating the object and call 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.

So, that is it. Laravel 5.5 Facades Tutorial here is over.

Leave A Reply

Your email address will not be published.