2

What is right way to send variables to the layout templete for it be approachable in error pages?

I have AppFrontController above all my frontend controllers. It have code (code is near) in onDispatch() method:

 $assocArrayOfVars = $this->MyPlugin()->getDbVariablesArray();
  foreach($assocArrayOfVars as $name => $value){
     $this->layout()->$name = $value;
  }

  list($catalog, $count_goods) = $this->MyPlugin()->getStandardCatalogDataForLayout();
  $this->layout()->catalog = $catalog;
  $this->layout()->count_goods = $count_goods;

As the result, I have my local variables in every frontend page. But I have’nt it in an error page. How I can to deside this problem? I very need your advices! Thank you!

  • `throw new CustomException($var1, $var2, $var3, $message, $code, $previous)` -> `class CustomException extends Exception { pub fun __construct(string $var1, string $var2, int $var3, $message = '', $code = null, Throwable $previous = null) { ...} }` -> Sry, had a few, should be it. Just make sure that you throw the correct status and that you have a default "Exception" handling in your specific error layout and a check in there if it's your custom exception type (`$error instanceof CustomException`). – rkeet Apr 20 '19 at 21:58
  • See example config here: https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Model_View_Controller/Error_Pages.html - Have a look in [Skeleton view/error folder](https://github.com/zendframework/ZendSkeletonApplication/tree/master/module/Application/view/error) for example on how it's usually handled. Modify to needs. – rkeet Apr 20 '19 at 22:01
  • error pages reads thrown exceptions by default. But if you want to add extra info (ex: session, cookie etc.) you have to listen MvcEvent::EVENT_RENDER. You can check if there's any exception thrown. Please check this question about [error handling](https://stackoverflow.com/questions/49984015/zend-framework-3-error-handling). – Mehmet SÖĞÜNMEZ May 09 '19 at 12:29

1 Answers1

0

Thank you for your advices! Problem is solved. Code of final version Module.php file below. I use listener instead of a “parent controller” by froschdesign advice.

 public function onBootstrap(MvcEvent $event)
   {
      $application = $event->getApplication();
      $eventManager = $application->getEventManager();
      $eventManager->attach('dispatch', array($this, 'loadConfiguration'), 2);
      $eventManager->attach('dispatch.error', array($this, 'loadConfiguration'), 2);
}


 public function loadConfiguration(MvcEvent $e)
   {
      $application = $e->getApplication();
      $sm = $application->getServiceManager();
      $sharedManager = $application->getEventManager()->getSharedManager();

      $router = $sm->get('router');
      $request = $sm->get('request');

      $zendCart = $sm->get('ControllerPluginManager')->get('ZendCart');
      $myPlugin = $sm->get('ControllerPluginManager')->get('MyPlugin');
      $viewModel = $e->getViewModel();

      $viewModel->setVariable('total', $zendCart->total());
      $viewModel->setVariable('total_items', $zendCart->total_items());

      $viewModel->setVariable('rusmonth', $rusmonth);

      /* Layout variables */
      $assocArrayOfVars = $myPlugin->getDbVariablesArray();
      foreach ($assocArrayOfVars as $name => $value) {
         $viewModel->setVariable($name, $value);
      }

      list($catalog, $count_goods) = $myPlugin->getStandardCatalogDataForLayout();
      $viewModel->setVariable('catalog', $catalog);
      $viewModel->setVariable('count_goods', $count_goods);

   }

More listener examples here.