The Controller is part of the MVC architecture (Action-Domain-Responder for web), responsible for further managing the handling of a request that has already been identified by the router, but should not contain business logic.
In the HLEB2 framework, controllers are regular handler classes bound to a route using the controller() method.
This method points to the controller class and its executable method.
Upon a match, the framework creates an instance of this class and calls the method.
The controller class must inherit from Hleb\Base\Controller.
The framework searches for the controller in the /app/Controllers/ folder according to its namespace. Here is the default controller code:
In the example, the controller's 'index' method returns a View object, created by the view() function and pointing to a template from the /resources/views/ folder.
The template /resources/views/default.php will be used
This is a simple example, as this function can be used similarly in a route.
The first argument of the function is the template, the second is a named array for passing variables and their values to the template, and the third argument can specify a numeric response status code.
If you use this example in a controller, the template /resources/views/template/file.php will be called.
In the file, the variables $title and $description will be available with their corresponding values:
In case the template file extension is not .php, for example, a .twig template, you need to rename the path to the template in the function, specifying the extension.
Besides the previously mentioned View object, other types of values can be returned from a controller method:
string|int|float - these types will be converted to a string and output in their original form as text.
array - the returned array will be converted to a JSON string.
bool - if false is returned, a standard 404 error will be displayed.
An object (from the container) with the Hleb\Reference\ResponseInterface interface will be converted to a response.
An object with the Psr\Http\Message\ResponseInterface interface will be converted to a response.
При ответе массивом как JSON на запрос GET может возникнуть XSSI перехват JSON, поэтому лучше отправить именованный массив, который преобразуется в объект как JSON.
Together with a dynamic route, values that match the named parts of the URL may be defined by the framework. For example, for the following route:
The variables $version and $page can be inserted into the 'resource' controller method.
One controller can return data from another, but the return data types must match.
No Events assigned to controllers will be applied to this nested controller.
Besides copying and modifying the demo file DefaultController.php, there is also a simple way to create a controller using a console command.
$php console --add controller ExampleController
This command will create a new controller template at /app/Controllers/ExampleController.php.
A different suitable name for the class can be used.
The framework also allows creating a custom default template for this command.