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

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