Laravel News
Follow
Learn how to create custom Facades in Laravel
Laravel Facades provide convenient access to services within the framework. Facades are a gateway to underlying services registered in the service container. Each facade has a getFacadeAccessor() method that reveals the registered service name.The DB Facade, for example, uses 'db' as its accessor. Some facades resolve to manager classes that dynamically delegate methods to underlying classes.Creating custom facades in Laravel allows for convenient access to frequently used services. The make:class command can be used to generate a facade class.Custom facades require defining a getFacadeAccessor() method to specify the service's name or class. They offer the advantage of direct mocking of the underlying service in tests.Alternatively, Laravel provides partialMock() for mocking services directly in tests. The choice between facades and dependency injection for testing depends on the developer's preference and the frequency of service usage.