Home > PHP Framework > Laravel > body text

What is the use of laravel facade?

WBOY
Release: 2022-02-15 11:08:51
Original
2028 people have browsed it

In laravel, facades are used to provide a static interface for the classes of the application's IoC service container. Laravel's facade serves as a static proxy for the underlying classes in the service container. Compared with traditional static methods, Provides easier-to-test syntax during maintenance.

What is the use of laravel facade?

The operating environment of this tutorial: Windows 10 system, Laravel 6 version, DELL G3 computer.

What is the use of laravel facade

Introduction

Facades provides a static interface for the application's IoC service container class. Laravel comes with some Facades, such as Cache, etc. Laravel's facade serves as a "static proxy" for the underlying class in the service container. Compared with traditional static methods, it can provide easier to test, more flexible, concise and elegant syntax during maintenance.

Explanation

In the context of Laravel application, a Facade is a class. Using this class, you can access an object from the container. This function is in the Facade defined in the class. Laravel's Facades and any Facades you define yourself will inherit the Facade class.

Your Facade class only needs to implement one method: getFacadeAccessor. Whatever needs to be resolved in the container is done in this method. The Facade base class uses the __callStatic() magic method, which can delay calls from Facade to the resolved object.

So, when you use Facade to call, like this: Cache:get, laravel will resolve the cache management class from the Ioc service container, and then call the get method on this class. Laravel's Facades can be used to locate services, which is a more convenient syntax for using Laravel's IoC service container.

Advantages

Facade has many advantages. It provides a simple and easy-to-remember syntax, allowing us to use Laravel provided without having to remember long class names. Functional features, in addition, make them easy to test due to their unique use of PHP's dynamic methods.

Actual use

The following example is used to call Laravel's cache system. Take a look at the following line of code first. You may think that this is a direct call to a static method called get on the Cache class.

$value = Cache::get('key');
Copy after login

However, if you look at the Illuminate\Support\Facades\Cache class, you will find that there is no get static method at all:

class Cache extends Facade {
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }
}
Copy after login

The Cache class inherits the Facade base class , which defines a method called getFacadeAccessor(). Note that what this method does is to return an Ioc binding name, here it is cache.

When the user references any static method on the Cache Facade, Laravel will resolve the cache binding from the Ioc service container and execute the requested method on the object. (Here is the get method).

So, when we call Cache::get, what it really means is this:

$value = $app->make('cache')->get('key');
Copy after login

Import Facades

Note, When using facade, if a namespace is used in the controller, you need to import the Facade class into this namespace. All Facades are under the global namespace:

<?php namespace App\Http\Controllers;
use Cache;
class PhotosController extends Controller {
    /**
     * Get all of the application photos.
     *
     * @return Response
     */
    public function index()
    {
        $photos = Cache::get(&#39;photos&#39;);
        //
    }
}
Copy after login

Create Facades

You only need three things to create a Facade:

  • An IoC binding.

  • A Facade class.

  • Configuration of a Facade alias.

Below we define a class: PaymentGateway\Payment.

namespace PaymentGateway;
class Payment {
    public function process()
    {
        //
    }
}
Copy after login

We need to be able to resolve this class in the Ioc service container. Therefore, first add a Service Provider binding:

App::bind(&#39;payment&#39;, function()
{
    return new \PaymentGateway\Payment;
});
Copy after login

The best way to register this binding is to create a new Service Provider, name it PaymentServiceProvider, and then bind it to register method. Then configure laravel and load your Service Provider in the configuration file config/app.php.

The next step is to create your own Facade class:

use Illuminate\Support\Facades\Facade;
class Payment extends Facade {
    protected static function getFacadeAccessor() {
             return &#39;payment&#39;; 
    }
}
Copy after login

Finally, if you like, you can add an alias to the Facade and put it in the aliases array in the config/app.php configuration file inside.

You can call the process method on an instance of the Payment class. Like this:

Payment::process();
Copy after login

[Related recommendations: laravel video tutorial]

The above is the detailed content of What is the use of laravel facade?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!