Facades
Introduction
In the Dreamfork documentation, you'll frequently come across code snippets that leverage Dreamfork's "facades" to interact with various features. Facades offer a "static" interface to classes registered in the application's service container. Dreamfork provides a range of facades, allowing convenient access to nearly all of framework functionalities.
Dreamfork facades act as "static proxies" to the underlying classes in the service container, delivering a concise and expressive syntax. This approach maintains superior testability and flexibility compared to traditional static methods.
All Dreamfork facades are neatly organized in the Framework\Support\Facades namespace. Consequently, accessing a facade is straightforward, as demonstrated below:
use Framework\Support\Facades\Hash;
$hash = Hash::make('raw_text');
Helper Functions
To complement facades, Dreamfork provides a range of convenient global "helper functions," streamlining the interaction with common Dreamfork features. These helper functions, such as view, response, url, config, and more, offer a simplified approach to various tasks. Each helper function in Dreamfork is thoroughly documented alongside its associated feature. You can find a comprehensive list in the dedicated helper documentation.
For instance, instead of utilizing the Framework\Support\Facades\Response facade to create a JSON response, you can effortlessly use the response function. Thanks to the global availability of helper functions, there's no need to import any classes before putting them to use:
use Framework\Support\Facades\Response;
return Response::json([$data]);
return response()->json([$data]);
When To Use Facades
Facades offer a short and memorable syntax, enabling you to utilize Dreamfork's features without the need to recall lengthy class names that require manual injection or configuration. Their use of PHP's dynamic methods also enhances ease of testing.
Facades Vs. Helper Functions
In addition to facades, Dreamfork provides a range of "helper" functions that handle common tasks such as generating views or sending HTTP responses. Many of these helper functions perform tasks equivalent to corresponding facades. For example, the following facade call and helper call are equivalent:
return Framework\Support\Facades\View::make('welcome');
return view('welcome');
There is no practical distinction between facades and helper functions. You can use them according to your preference when a corresponding helper function is defined for a given facade.
How Facades Work
In the Dreamfork framework, a facade serves as a convenient way to access objects from the container. The underlying mechanics are implemented in the Facade class, with Dreamfork's facades extending the base Framework\Support\Facades\Facade class.
The Facade class utilizes the __callStatic() magic method, deferring calls from the facade to an object resolved from the container. Consider the following example, where a call is made to Dreamfork's hash service:
use Framework\Support\Facades\Hash;
$hash = Hash::make('raw_text');
At first glance, it might appear that the static make method is being called on the Hash class. However, this facade acts as a proxy, providing access to the underlying implementation of the Framework\Services\Hash\HashManager class. Any calls made through the facade are forwarded to the underlying instance of Dreamfork's hash manager.
In the case of the Framework\Support\Facades\Hash class, you won't find a static method named make. Instead, the Hash facade extends the base Facade class and defines the getFacadeAccessor() method. This method returns the name of a service container binding. When a user invokes any static method on the Hash facade, Dreamfork resolves the hash manager binding from the service container and executes the requested method (such as make) on that object.
Facade Class Reference
Below you will find every facade and its underlying class. The service container binding key is also included where applicable.
| Facade | Class | Service Container Binding | Helper Method |
|---|---|---|---|
| Auth | Framework\Services\Auth\AuthManager | auth |
|
| DB | Framework\Database\DatabaseManager | db |
|
| Storage | Framework\Filesystem\FilesystemManager | filesystem |
|
| Hash | Framework\Services\Hash\HashManager | hash |
|
| Request | Framework\Http\Request | request |
request() |
| Response | Framework\Http\Response | response |
response() |
| Route | Framework\Http\Router | route |
router() |
| URL | Framework\Services\URL\UrlGenerator | url |
url() |
| Validator | Framework\Services\Validator\Factory | validator |
validator() |
| View | Framework\View\Factory | view |
view() |