2016-05-08 19 views
0

Wie Vorlage vor dem Rendern in Zend Expressive überprüfen? Hier ist meine Aktion:Wie überprüft man die Existenz einer Vorlage vor dem Rendern in 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'))); 
    } 
} 

Ich bin neu in ZE. Habe keine Ahnung wie man das macht.

Antwort

1

Soweit ich weiß, gibt es keine Möglichkeit zu überprüfen, ob eine Vorlage existiert. Wenn eine Vorlage nicht gefunden wird, wird eine Ausnahme ausgelöst.

Die beabsichtigte Art, es zu verwenden, besteht darin, für jede Aktion eine Vorlage zu erstellen.

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. Aktion:

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

Es funktioniert, wenn Sie nicht faul sind :) Zwei Aktionen ist nicht Problem :) P. S. Ich benutze file_exists(). –