Zend Expressive, error page, send data to it

I need to send specific app data to the error, and 404 templates, which get rendered automatically, when an error happens;

  • as kindly being suggested on slack i wrote a factory that calls to the Zend\Expressive\Plates\PlatesRendererFactory, and then, on the obtained plates renderer it injects default template data:
  • the problem is that some of the data that i need in the error template is in the request object, and i do not know how to access it in the factory;
  • can anyone give me a clue ?

Thank you.

I found the solution by using another idea from slack, from (Matthew Weier) @matthew

  • i wrote a middleware, that gets created by a factory (as usual);

  • in the factory:
    – gets some data from the container (data which is necessary in the error template) and sends it to the constructor of the middleware, and it also sends the template renderer, obtained like this:

    $templateRenderer = $container->has(\Zend\Expressive\Template\TemplateRendererInterface::class)
    ? $container->get(\Zend\Expressive\Template\TemplateRendererInterface::class)
    : null;
    $someVar1 = $container->has(‘someVar1’)? $container->get(‘someVar1’) : null;
    $someVar2 = $container->has(‘someVar2’)? $container->get(‘someVar2’) : null;

    – the middleware gets created with the necessary template data
    return new \App\Middleware\DecorateTemplateRendererMiddleware($templateRenderer, $someVar1, $someVar2);

  • in the middleware:
    – i get other data that i need from the request;
    – i put all the data necessary for the error template as default parameters in the template renderer like this:


– in the error, and 404 templates (templates/error/error.phtml and templates/error/404.phtml) i have the variables $var1 and $var2 available