51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Core\I18n;
|
|
use App\Core\Locale;
|
|
use App\Core\View;
|
|
|
|
/**
|
|
* Base controller — injects i18n globals (locale, t() helper, locale switcher)
|
|
* into every render() call so views can use `$t('key')` and `$locale` directly.
|
|
*/
|
|
abstract class Controller
|
|
{
|
|
protected View $view;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->view = new View();
|
|
}
|
|
|
|
/**
|
|
* Render a view inside a layout.
|
|
*
|
|
* @param array<string,mixed> $data
|
|
*/
|
|
protected function render(string $view, array $data = [], string $layout = 'main'): void
|
|
{
|
|
$locale = LocaleController::current();
|
|
$i18n = static fn (string $key, array $params = []): string => I18n::t($key, $params, $locale);
|
|
|
|
$globals = [
|
|
'locale' => $locale,
|
|
't' => $i18n,
|
|
'locale_switcher' => static function (string $currentPath) use ($locale): string {
|
|
$switcher = new LocaleSwitcher($locale, $currentPath);
|
|
return $switcher->render();
|
|
},
|
|
];
|
|
|
|
$merged = array_merge($globals, $data);
|
|
|
|
foreach ($merged as $key => $value) {
|
|
$this->view->assign($key, $value);
|
|
}
|
|
$this->view->render($view, $layout);
|
|
}
|
|
}
|