Facades
Introduction
facades provide a "static" interface for the core functionality of the application and provide a more flexible, more elegant, and easy-to-test syntax. All facades of Goravel are defined under the app/facades folder:
import "app/facades"
facades.Config().GetString("app.host")How Facades Work
Each service provider registers its corresponding bindings in the service container, then the service container providers vairous Make* functions to build the binding instances. The facades in the app/facades folder call these Make* functions to get the instances from the service container. Let's use the Route facade as an example:
- The
Routeservice provider registers thebinding.Routebinding in the service container:
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) {}- The
Routefacade calls theMakeRoute()function to get theRouteinstance from the service container:
// app/facades/route.go
package facades
import (
"github.com/goravel/framework/contracts/route"
)
func Route() route.Route {
return App().MakeRoute()
}Given that the
facadesis exposed to the application, you can also create your ownfacadesor override the existingfacadesin theapp/facadesfolder.
Install/Uninstall Facades
goravel/goravel installs all facades by default and goravel/goravel-lite only installs essential facades like Artisan, Config. You can install or uninstall other facades as needed via the package:install and package:uninstall commands.
# Install a specific facade
./artisan package:install Route
# Install all facades
./artisan package:install --all
# Install all facades with default drivers
./artisan package:install --all --default
# Uninstall a specific facade
./artisan package:uninstall RouteNotice: if you are using the
./artisan package:installcommand to choose thefacadesmanually, you need to pressxto select the facades you want to install, then pressEnterto confirm.facadesare not selected if you directly pressEnter.
