Как проверить наличие шаблона перед рендерингом в Zend Expressive?

Как проверить шаблон перед рендерингом в Zend Expressive? Вот мое действие:

class Section
{
private $container;
private $template;

public function __construct(ContainerInterface $container, Template\TemplateRendererInterface $template = null)
{
$this->container = $container;
$this->template  = $template;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
if (false === 'Exist or Not') {
return $next($request, $response->withStatus(404), 'Not found');
}

return new HtmlResponse($this->template->render('app::'.$request->getAttribute('path')));
}
}

Я новичок в ZE. Понятия не имею, как это сделать.

0

Решение

Насколько я знаю, нет способа проверить, существует ли шаблон. Если шаблон не найден, он генерирует исключение.

Предполагаемый способ его использования — создание шаблона для каждого действия.

class PostIndexAction
{
private $container;
private $template;

public function __construct(ContainerInterface $container, Template\TemplateRendererInterface $template = null)
{
$this->container = $container;
$this->template  = $template;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new HtmlResponse($this->template->render('app::post-index'));
}
}

2-е действие:

class PostViewAction
{
private $container;
private $template;

public function __construct(ContainerInterface $container, Template\TemplateRendererInterface $template = null)
{
$this->container = $container;
$this->template  = $template;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
return new HtmlResponse($this->template->render('app::post-view'));
}
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]