Facades
简介
facades 为应用的核心功能提供一个「静态」接口,能够提供更加灵活、更加优雅、易于测试的语法。 Goravel 的所有 facades 都定义在 app/facades 文件夹下:
go
import "app/facades"
facades.Config().GetString("app.host")facades 工作原理
每个服务提供者在服务容器中注册绑定,然后是服务容器提供很多 Make* 方法来构建服务提供者绑定的实例。 app/facades 文件夹中的 facades 通过调用 Make* 方法从服务容器中获取实例。 以 Route facade 为例:
Route服务提供者注册在服务容器中注册binding.Route绑定:
go
type ServiceProvider struct {}
func (r *ServiceProvider) Register(app foundation.Application) {
app.Singleton(binding.Route, func(app foundation.Application) (any, error) {
return NewRoute(app.MakeConfig())
})
}
func (r *ServiceProvider) Boot(app foundation.Application) {}Routefacade 调用MakeRoute()函数从服务容器中获取Route实例:
go
// app/facades/route.go
package facades
import (
"github.com/goravel/framework/contracts/route"
)
func Route() route.Route {
return App().MakeRoute()
}
facades被暴露于应用程序中,你也可以在app/facades文件夹中创建自己的facades或覆盖现有的facades。
安装/卸载 Facades
goravel/goravel 默认安装所有 facades,goravel/goravel-lite 只安装Artisan、Config 这样的基本 facades。 你可以通过 package:install 和 package:uninstall 命令安装或卸载 facades 。
shell
# 安装指定的 facade
./artisan package:install Route
# 安装所有 facades
./artisan package:install --all
# 使用默认驱动安装所有 facades
./artisan package:install --all --default
# 卸载指定的 facade
./artisan package:uninstall Route