0

如何在 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的新手。不知道如何做到这一点。

4

1 回答 1

1

据我所知,没有办法检查模板是否存在。如果找不到模板,则会引发异常。

使用它的预期方法是为每个操作创建一个模板。

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'));
    }
}

第二个动作:

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'));
    }
}
于 2016-05-19T10:37:36.310 回答