laravel facades——迹忆客-ag捕鱼王app官网
简介
facade 为应用程序的服务容器中可用的类提供“静态”接口。laravel自带了许多facade,可以说几乎覆盖了laravel的所有功能。laravel facade作为服务容器中基础类的“静态代理”,具有简洁,表达性强的语法的优点,同时比传统静态方法具有更高的可测试性和灵活性。
laravel的所有facade都在illuminate\support\facades
命名空间中定义。因此,我们可以像这样轻松访问 facade:
use illuminate\support\facades\cache;
route::get('/cache', function () {
return cache::get('key');
});
在整个laravel文档中,许多示例都将使用facades来演示框架的功能。
何时使用 facade
facade 有很多好处。它们提供了一种简洁且容易记的语法,使我们可以使用laravel的功能而无需记住必须手动注入或配置的那么长的类名。此外,由于它们使用了php动态方法,因此也易于测试。
但是,在使用 facade 时必须格外小心。facade 的主要危险是类范围蠕变。由于 facade 是如此易于使用且不需要注入,因此可以很容易地让我们的类变得过大,并且很容易在一个类中使用许多fcacade。因此,在使用facade时,应特别注意类的大小,尽量使类的职责单一。
构建与laravel交互的第三方程序包时,最好注入laravel contract,而不要使用facade。由于软件包是在laravel本身之外构建的,因此我们将无法使用laravel的facade测试函数。
facade 与 依赖注入
依赖项注入的主要好处之一是可以改变所注入类的实现。这在测试期间很有用,因为我们可以注入一个 mock 或 stub 并对stub上的各种方法进行断言。
通常,不可能mock或stub真正的静态类方法。但是,由于facade使用动态方法将代理调用服务容器中解析的对象的方法,因此实际上我们可以像测试注入的类实例一样测试facade。例如,给定以下路由:
use illuminate\support\facades\cache;
route::get('/cache', function () {
return cache::get('key');
});
我们可以编写下面测试来验证cache::get是否使用了我们期望的参数调用的:
use illuminate\support\facades\cache;
/**
* a basic functional test example.
*
* @return void
*/
public function testbasicexample()
{
cache::shouldreceive('get')
->with('key')
->andreturn('value');
$this->visit('/cache')
->see('value');
}
facade 与 辅助功能
除了 facade 外,laravel还包括各种“辅助”功能,可以执行常见的任务,例如生成视图,触发事件,调度作业或发送http响应。这些帮助程序中的许多功能与相应的 facade 具有相同的功能。例如,下面facade调用和函数调用是等效的:
return view::make('profile');
return view('profile');
facade 和 辅助功能之间没有实际区别。使用辅助函数时,我们仍然可以像对相应facade进行测试一样准确地对其进行测试。例如,给定以下路由:
route::get('/cache', function () {
return cache('key');
});
cache帮助程序调用cache facade类中的get。因此,即使我们正在使用辅助函数,我们也可以编写以下测试来验证是否使用我们期望的参数调用了该方法:
use illuminate\support\facades\cache;
/**
* a basic functional test example.
*
* @return void
*/
public function testbasicexample()
{
cache::shouldreceive('get')
->with('key')
->andreturn('value');
$this->visit('/cache')
->see('value');
}
facade 如何工作
在laravel应用程序中,facade是提供从容器中访问对象的类。laravel自带的facade 以及我们创建的任何自定义 facade 都将继承illuminate\support\facades\facade
基类。
facade基类利用__callstatic()
魔术方法来延迟facade对从容器解析出来的对象的调用。在下面的示例中,调用了laravel缓存系统。通过看一下这段代码,可以假定在调用cache类上的静态方法get:
$user]);
}
}
请注意,在文件顶部附近,我们正在“导入”cache facade。此facade充当访问illuminate\contracts\cache\factory
接口的基础实现的代理。我们使用 facade 进行的所有调用都将传递给laravel缓存服务的基础实例。
如果我们查看illuminate\support\facades\cache
类,就会看到实际是没有静态方法get的:
class cache extends facade
{
/**
* get the registered name of the component.
*
* @return string
*/
protected static function getfacadeaccessor() { return 'cache'; }
}
相反,cache facade继承了facade基类并定义了getfacadeaccessor()
方法。该方法的工作是返回服务容器绑定的实例名称。当用户引用cache facade上的任何静态方法时,laravel都会从服务容器解析绑定的实例cache,并执行请求该对象的 get 方法。
实时 facade
使用实时 facade ,我们可以将应用程序中的任何类都视为facade。为了说明如何使用它,让我们研究一个替代方法。例如,假设我们的podcast模型有一个publish方法。但是,为了发布播客,我们需要注入一个publisher实例:
update(['publishing' => now()]);
$publisher->publish($this);
}
}
将publisher
实现注入到方法中,因为我们可以模拟注入的发布者,因此我们可以轻松地单独测试该方法。但是,它要求我们每次调用该publish方法时始终传递一个 publisher 实例。使用实时facade,我们可以保持相同的可测试性,而无需显式传递publisher实例。要生成实时facade,需要在导入的类的命名空间添加前缀facades:
update(['publishing' => now()]);
publisher::publish($this);
}
}
使用实时 facade 时,将使用出现在facades前缀之后的接口或类名称的一部分,将 publisher 实现从服务容器中解析出来。在测试时,我们可以使用laravel的内置 facade 测试方法来模拟此方法调用:
create();
publisher::shouldreceive('publish')->once()->with($podcast);
$podcast->publish();
}
}
facade 类参考
在下面,我们将找到每个facade及其基础类。
facade | 对应的类 | 服务容器中绑定的名称 |
---|---|---|
app | illuminate\foundation\application | app |
artisan | illuminate\contracts\console\kernel | artisan |
auth | illuminate\auth\authmanager | auth |
auth (instance) | illuminate\contracts\auth\guard | auth.driver |
blade | illuminate\view\compilers\bladecompiler | blade.compiler |
broadcast | illuminate\contracts\broadcasting\factory | |
broadcast (instance) | illuminate\contracts\broadcasting\broadcaster | |
bus | illuminate\contracts\bus\dispatcher | |
cache | illuminate\cache\cachemanager | cache |
cache (instance) | illuminate\cache\repository | cache.store |
config | illuminate\config\repository | config |
cookie | illuminate\cookie\cookiejar | cookie |
crypt | illuminate\encryption\encrypter | encrypter |
db | illuminate\database\databasemanager | db |
db (instance) | illuminate\database\connection | db.connection |
event | illuminate\events\dispatcher | events |
file | illuminate\filesystem\filesystem | files |
gate | illuminate\contracts\auth\access\gate | |
hash | illuminate\contracts\hashing\hasher | hash |
http | illuminate\http\client\factory | |
lang | illuminate\translation\translator | translator |
log | illuminate\log\logmanager | log |
illuminate\mail\mailer | mailer | |
notification | illuminate\notifications\channelmanager | |
password | illuminate\auth\passwords\passwordbrokermanager | auth.password |
password (instance) | illuminate\auth\passwords\passwordbroker | auth.password.broker |
queue | illuminate\queue\queuemanager | queue |
queue (instance) | illuminate\contracts\queue\queue | queue.connection |
queue (base class) | illuminate\queue\queue | |
redirect | illuminate\routing\redirector | redirect |
redis | illuminate\redis\redismanager | redis |
redis (instance) | illuminate\redis\connections\connection | redis.connection |
request | illuminate\http\request | request |
response | illuminate\contracts\routing\responsefactory | |
response (instance) | illuminate\http\response | |
route | illuminate\routing\router | router |
schema | illuminate\database\schema\builder | |
session | illuminate\session\sessionmanager | session |
session (instance) | illuminate\session\store | session.store |
storage | illuminate\filesystem\filesystemmanager | filesystem |
storage (instance) | illuminate\contracts\filesystem\filesystem | filesystem.disk |
url | illuminate\routing\urlgenerator | url |
validator | illuminate\validation\factory | validator |
validator (instance) | illuminate\validation\validator | |
view | illuminate\view\factory | view |
view (instance) | illuminate\view\view |