Documentation/Controllers/Controller

Controller

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:

<?php

namespace App\Controllers;

use 
Hleb\Base\Controller;
use 
Hleb\Constructor\Data\View;

class 
DefaultController extends Controller
{
    public function 
index(): View
    
{
        return 
view("default");
    }
}

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.


#view() Function

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.

view('/template/file', ['title' => 'Main template''description' => 'Template description'], 205);

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:

<?php
// File /resources/views/template/file.php

/**
 * @var string $title
 * @var string $description
 */
echo $title// Main template
echo $description// Template description

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.


#Return Values

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.


#Inserting Dynamic Variables

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:

use App\Controllers\DefaultController;

Route::get('/resource/{version}/{page?}/')
    ->
where(['version' => '[0-9]+''page' => '[a-z]+'])
    ->
controller(DefaultController::class, 'resource');

The variables $version and $page can be inserted into the 'resource' controller method.

<?php

namespace App\Controllers;

use 
Hleb\Base\Controller;

class 
DefaultController extends Controller
{
    public function 
resource(int $version, ?string $page null): void
    
{
        
// ... //
    
}
}

#Using Another Controller

One controller can return data from another, but the return data types must match.

<?php

namespace App\Controllers;

use 
Hleb\Base\Controller;

class 
DefaultController extends Controller
{
    public function 
index(): mixed
    
{
        return (new 
OtherController($this->config))->index();
    }
}

No Events assigned to controllers will be applied to this nested controller.


#Creating a 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.

Routing Module

Page translated: chatgpt 4-o
Back to top