-
Laravel 的契约是一组定义框架提供的核心服务的接口。框架对每个契约都提供了相应的实现。
-
所有的 Laravel 契约都有他们自己的 GitHub 库。
-
在大多数情况下,每个 Facades 都有一个等效的契约。
-
对于大多数应用程序来说不管是使用门面还是契约都可以。但是,如果你正在构建一个扩展包,为了方便测试,你应该强烈考虑契约。
-
可以将契约视为说明框架功能的简洁文档。
-
要获得一个契约的实现,你只需要被解析的类的构造函数中添加「类型提示」即可。
<?php
namespace App\Listeners;
use App\User;
use App\Events\OrderWasPlaced;
use Illuminate\Contracts\Redis\Database;
class CacheOrderInformation
{
/**
* Redis 数据库实现。
*/
protected $redis;
/**
* 创建事件处理器实例。
*
* @param Database $redis
* @return void
*/
public function __construct(Database $redis)
{
$this->redis = $redis;
}
/**
* 处理事件。
*
* @param OrderWasPlaced $event
* @return void
*/
public function handle(OrderWasPlaced $event)
{
//
}
}
- 契约参考
Contract | References Facade |
---|---|
Illuminate\Contracts\Auth\Factory | Auth |
Illuminate\Contracts\Auth\PasswordBroker | Password |
Illuminate\Contracts\Bus\Dispatcher | Bus |
Illuminate\Contracts\Broadcasting\Broadcaster | |
Illuminate\Contracts\Cache\Repository | Cache |
Illuminate\Contracts\Cache\Factory | Cache::driver() |
Illuminate\Contracts\Config\Repository | Config |
Illuminate\Contracts\Container\Container | App |
Illuminate\Contracts\Cookie\Factory | Cookie |
Illuminate\Contracts\Cookie\QueueingFactory | Cookie::queue() |
Illuminate\Contracts\Encryption\Encrypter | Crypt |
Illuminate\Contracts\Events\Dispatcher | Event |
Illuminate\Contracts\Filesystem\Cloud | |
Illuminate\Contracts\Filesystem\Factory | File |
Illuminate\Contracts\Filesystem\Filesystem | File |
Illuminate\Contracts\Foundation\Application | App |
Illuminate\Contracts\Hashing\Hasher | Hash |
Illuminate\Contracts\Logging\Log | Log |
Illuminate\Contracts\Mail\MailQueue | Mail::queue() |
Illuminate\Contracts\Mail\Mailer | |
Illuminate\Contracts\Queue\Factory | Queue::driver() |
Illuminate\Contracts\Queue\Queue Queue | |
Illuminate\Contracts\Redis\Factory | Redis |
Illuminate\Contracts\Routing\Registrar | Route |
Illuminate\Contracts\Routing\ResponseFactory | Response |
Illuminate\Contracts\Routing\UrlGenerator | URL |
Illuminate\Contracts\Support\Arrayable | |
Illuminate\Contracts\Support\Jsonable | |
Illuminate\Contracts\Support\Renderable | |
Illuminate\Contracts\Validation\Factory | Validator::make() |
Illuminate\Contracts\Validation\Validator | |
Illuminate\Contracts\View\Factory | View::make() |
Illuminate\Contracts\View\View |
网友评论