Facade/Appearance mode (Facade)


In order to facilitate developers, the framework has encapsulated many of the most commonly used system methods in advance through facade technology.

If we define an app\common\Test class, There is a hello dynamic method inside.

<?php
namespace app\common;

class Test
{
    public function hello($name)
    {
        return 'hello,' . $name;
    }
}

The code for calling the hello method should be similar to:

$test = new \app\common\Test;
// 输出 hello,thinkphp
echo $test->hello('thinkphp');

We define a static proxy class app\facade\Test for this class (the class name is arbitrary, but keeping it unified will facilitate maintenance).

<?php
namespace app\facade;

use think\Facade;

class Test extends Facade
{
    protected static function getFacadeClass()
    {
    	return 'app\common\Test';
    }
}

As long as this class library inherits think\Facade, you can use the static method to call the dynamic method of the dynamic class app\common\Test. For example, the above code can be changed to:

// 无需进行实例化 直接以静态方法方式调用hello
echo \app\facade\Test::hello('thinkphp');

Core Facade Class Library

The system defines Facade class libraries for built-in commonly used class libraries, including:


##(Dynamic) Class LibraryFacade Class##think\Appthink\Cachethink\Configthink\Cookiethink\Dbthink\Envthink\Eventthink\Filesystemthink\Langthink\Logthink\Middlewarethink\Requestthink\Response think\Routethink \Sessionthink\Validatethink\View
think\facade\App
think\facade\Cache
think\facade\ Config
think\facade\Cookie
think\ facade\Db
think\facade\Env
think\facade\Event
think\facade\Filesystem
think\facade\Lang
think\facade\Log
think\facade\Middleware
think\facade\Request
think\facade\Response
think\facade\Route
think\facade\Session
think\facade\Validate
think\facade\View


So you don’t need to instantiate it and it’s very convenient Make method calls, for example:

use think\facade\Cache;

Cache::set('name','value');
echo Cache::get('name');

When performing dependency injection, please do not use the Facade class as a type constraint. Instead, it is recommended to use the original dynamic class. The following is the incorrect usage:

<?php
namespace app\index\controller;

use think\facade\App;

class Index
{
    public function index(App $app)
    {
    }
}

The following method should be used:

<?php
namespace app\index\controller;

use think\App;

class Index
{
    public function index(App $app)
    {
    }
}

In fact, the effect of dependency injection and using Facade proxy is the same in most cases, both of which are to obtain object instances from the container. For example:

<?php
namespace app\index\controller;

use think\Request;

class Index
{
    public function index(Request $request)
    {
        echo $request->controller();
    }
}

has the same effect as the following

<?php
namespace app\index\controller;

use think\facade\Request;

class Index
{
    public function index()
    {
        echo Request::controller();
    }
}

In actual development, it is recommended to use: dependency injection