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 provides various 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:
package facades
import (
"github.com/goravel/framework/contracts/route"
)
func Route() route.Route {
return App().MakeRoute()
}TIP
Because facades are exposed to the application, you can create your own facades or override the existing ones in the app/facades folder.
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 RouteTIP
If you are using the ./artisan package:install command to choose the facades manually, you need to press x to select the facades you want to install, then press Enter to confirm. facades are not selected if you directly press Enter.