Home > PHP Framework > Laravel > body text

Detailed interpretation of Laravel Facade

藏色散人
Release: 2020-11-09 15:52:46
forward
2275 people have browsed it

The following is a detailed explanation of Laravel Facade from the Laravel tutorial column. Hope it helps those in need!

Detailed interpretation of Laravel Facade

Hello everyone, today’s content is about the implementation principle of Laravel’s Facade mechanism.

Simple use of Facade

Use of database:

$users = DB::connection('foo')->select(...);
Copy after login

IOC container

As we all know, the IOC container is the most important part of the Laravel framework. It provides two functions, IOC and containers.

  • IOC (Inversion of Control), also called inversion of control. To put it bluntly, it is to control the generation of objects so that developers do not need to care about how the objects are generated, but only need to care about their use.
  • The object instance generated through the IOC mechanism needs a storage location for continued use, which is its container function.

This time I am not going to explain the specific implementation of the IOC container. There will be articles explaining it in detail later. Regarding IOC containers, readers only need to remember two points:

  1. Generate object instances according to configuration;
  2. Save object instances for easy access at any time;

Simplified Facade class

<?php
namespace facades;

abstract class Facade
{
    protected static $app;

    /** 
     * Set the application instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    public static function setFacadeApplication($app)
    {   
        static::$app = $app;
    } 

    /** 
     * Get the registered name of the component.
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    protected static function getFacadeAccessor()
    {
        throw new RuntimeException(&#39;Facade does not implement getFacadeAccessor method.&#39;);
    }

    /** 
     * Get the root object behind the facade.
     *
     * @return mixed
     */
    public static function getFacadeRoot()
    {   
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    } 

    /**
     * Resolve the facade root instance from the container.
     *
     * @param  string|object  $name
     * @return mixed
     */
    protected static function resolveFacadeInstance($name)
    {
        return static::$app->instances[$name];
    }

    public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        if (! $instance) {
            throw new RuntimeException(&#39;A facade root has not been set.&#39;);
        }   

        switch (count($args)) {
            case 0:
                return $instance->$method();
            case 1:
                return $instance->$method($args[0]);
            case 2:
                return $instance->$method($args[0], $args[1]);
            case 3:
                return $instance->$method($args[0], $args[1], $args[2]);
            case 4:
                return $instance->$method($args[0], $args[1], $args[2], $args[3]);
            default:
                return call_user_func_array([$instance, $method], $args);
        } 
    }
}
Copy after login

Code description:

  1. $app stores an IOC container instance, which is passed through setFacadeApplication( ) The
  2. set by this static method implements the __callStatic magic method
  3. The getFacadeAccessor() method requires subclasses to inherit and returns a string identifier. Through this identifier, the IOC container can return The object of the class it is bound to (framework initialization or binding at other times)
  4. Call specific methods through $instance

Create your own Facade:

TEST1 The specific logic:

<?php
class Test1{
	public function hello()
	{
		print("hello world");
	}}
Copy after login

Facade of the TEST1 class:

<?php
namespace facades;/**
 * Class Test1
 * @package facades
 *
 * @method static setOverRecommendInfo [设置播放完毕时的回调函数]
 * @method static setHandlerPlayer [明确指定下一首时的执行类]
 */class Test1Facade extends Facade{
    protected static function getFacadeAccessor()
    {   
        return &#39;test1&#39;;
    }   }
Copy after login

Usage:

use facades\Test1Facade;Test1Facade::hello();  // 这是 Facade 调用
Copy after login

Explanation:

  1. facades\Test1Facade calls the static method hello (), since this method is not defined, __callStatic will be called;
  2. In __callStatic, the corresponding instance is first obtained, that is, return static::$app->instances[$name ];. The $name is the test1$app in
  3. facades\Test1
  4. , which is the IOC container, and the instantiation process of the class is handed over to it to handle.

The above is the detailed content of Detailed interpretation of Laravel Facade. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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!