There is a task to send email via console. Part of the contents of the letter - statistics - is formed through the ViewHelper "accountsStat". ViewHelper itself “accountsStat” also uses other ViewHelper's (for example, “decodeUrl”).

As I understand it, the problem is that when you start the application through the console, ZF2 does not initialize the View, as it does when it starts via http.

Therefore, inside the ViewHelper "accountsStat" empty $ this-> getView ().

Of course, when working with the console, you can initialize the View for each ViewHelper.

But as I already wrote, in the accountsStat.phtml itself, other ViewHelperes are used, which also results in the need to cling to the View yourself ...

I would like to know if this problem can be solved in a complex way: i. configure based on the "view_manager" file "module.config.php" View (+ taking into account \ Application \ Module-> getViewHelperConfig ()) for all ViewHelper'ov, as it would be for http?

  • Do you want to render a view templi from the console? - newage
  • Yes + ViewHelpers + ViewHelpers in ViewHelpers :) - Denis Bolmazov
  • The Zend \ Mvc \ View \ ConsoleViewManager has a getMvcRenderingStrategy. If you change the 82nd line to $ this-> mvcRenderingStrategy = new \ Zend \ Mvc \ View \ Http \ DefaultRenderingStrategy ($ this-> getView ()), everything starts working! It remains to figure out how to do this without changes in the core :( - Denis Bolmazov
  • As you need from the application, the DefaultRenderingStrategy service should be changed from \ Zend \ Mvc \ View \ Console \ DefaultRenderingStrategy to \ Zend \ Mvc \ View \ Http \ DefaultRenderingStrategy - Denis Bolmazov
  • Perhaps this link will help. You can add your event and change the strategy. framework.zend.com/manual/2.4/en/modules.1 ... - newage

2 answers 2

From the service locator, you can call the renderer, all the dependencies for the renderer will be initialized.

$renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer'); $view = new ViewModel(); $renderer->render($view); 
  • I couldn’t be able to create an instance for \ Zend \ View \ Renderer \ PhpRenderer - Denis Bolmazov
  • It is necessary to deal with the settings. I can not say why you can not see the standard objects in the service locator. - newage

I do not know how to comment on this ... apparently I’m as stupid as stubborn, but ... I just add to

Module.php

 public function onBootstrap(MvcEvent $e){ ... if ($e->getRequest() instanceof \Zend\Console\Request) { $e->getApplication()->getServiceManager()->get('ViewManager')->getView(); } ... } 

And I calmly work with View and ViewHelper'ami on the console, as well as on http ... Why? I do not know. But while it works!