Facades and Contracts

04/01/2022   Laravel

Facades and Contracts

1. Contracts
- is Interface for other class using (implement or inject)
- Allmost services in Laravel using Contracts to develop services
* Contracts là Interface cho các Class khác sử dụng (implement or inject), 

2. Facades
is Class to call method for using methods in container services of of laravel
* Facades là Class dùng để gọi các methods của các class (services) trong container của laravel.
------------------
* /vendor/laravel/framework/src/Illuminate/Support/Facades/Log.php
* @see \Illuminate\Log\Logger
class Log extends Facade

3. Ứng dụng
Từ Facades, Contracts => tất cả các methods có thể sử dụng của Object (Class).
------------------

Facades: https://laravel.com/docs/9.x/facades
Contracts: https://laravel.com/docs/9.x/contracts

==========================================================

- Facades là class, có method getFacadeAccessor(), để gọi 1 class (instance) trong service container có các method
=> sử dụng: https://laravel.com/docs/6.x/facades#how-facades-work chỉ cần gọi method: ex: return Cache::get('key'); 

- a facade is a class that provides access to an object from the container

- Contracts là interfaces, trong service container có các class (instance) implement sẵn các Contracts này.
=> sử dụng: https://laravel.com/docs/6.x/contracts#how-to-use-contracts, So, to get an implementation of a contract, you can just "type-hint" the interface in the constructor of the class being resolved

 

Bài viết cùng chủ đề