Bicycle/System/Classes/Controller.php
Egor Isaev b65603b4d0 dev
2026-06-23 10:44:18 +03:00

42 lines
1.5 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description Controller.php
* @copyright (c) 03/06/2026
*/
namespace System\Classes;
/**
* Базовый контроллер. render() строит двухуровневый вывод: layout + content.
*/
class Controller
{
/** @var string Имя layout-шаблона в System/view/views */
protected string $_layout = 'layout';
/**
* Рендерит шаблон контента внутри layout.
* Если $dir пуст — определяется автоматически из имени класса
* (App\Controller\FooController → view/Foo).
*
* @param string $template Имя шаблона контента без расширения
* @param array $data Данные, передаваемые в шаблон
* @param string $dir Каталог шаблона относительно view/ (опционально)
* @return string Готовый HTML
* @throws MyException
*/
protected function render(string $template, array $data = [], string $dir = ''): string
{
if ($dir === '') {
$class = substr(get_class($this), strlen('App\\Controller\\')); // [Admin\]FooController
$dir = 'view/' . str_replace('\\', '/', substr($class, 0, -10)); // view/[Admin/]Foo
}
return (new View($this->_layout, 'view/views', [
'content' => (new View($template, $dir, $data))->render()
]))->render();
}
}