2016-05-08 7 views
0

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に新しいです:ここに私の行動です。これを行う方法はありません。

答えて

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

あなたは怠惰でない場合、それは動作します:)二つのアクションは問題ではありません:) P.S.私はfile_exists()を使用します。 –

関連する問題