0
votes

I have the problem with doctrine console. I have a MailServiceFactory.php containing this code:

namespace Application\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MailServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $I_render = $serviceLocator->get('ViewRenderer');

        $a_config = $serviceLocator->get('config');

        return new MailService($I_render, $a_config);
    }
}
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MailServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $I_render = $serviceLocator->get('ViewRenderer');

        $a_config = $serviceLocator->get('config');

        return new MailService($I_render, $a_config);
    }
}

and all functions works, but when i execute the vendor/bin/doctirne-module I get the error below:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for ViewRenderer' in /Users/Daniele/Apps/corradini.com/www/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:529

Why i'm getting this error?

4

4 Answers

0
votes

You cannot get ViewRenderer directly from ServiceLocator on console requests because its not in stage since request is not an HttpRequest.

Instead of ViewRenderer, you can easily try to passing a PhpRenderer instance to your service and render a ViewModel using that. For example:

use Zend\View\Renderer\PhpRenderer;

class MailServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $renderer = new PhpRenderer();
        $a_config = $serviceLocator->get('config');

        return new MailService($renderer, $a_config);
    }

Now in your MailService:

use Zend\View\Renderer\RendererInterface;
use Zend\View\Model\ViewModel;

class MailService
{
   protected $renderer;

   public function __construct(RendererInterface $renderer, $config)
   {
       $this->renderer = $renderer;
       // set your config etc..
   }

   public function fooMethod()
   {
       $model = new ViewModel();
       $model->setTemplate('path/to/template.phtml');
       $result = $this->renderer->render($model);
       // The $result contains rendered html markup
   }

Hope it helps.

0
votes

Thanks foozy now doctrine console funciton! but the i can't read the tempalte for render mail.

My tempate is locatend in module/Application/view/email/template.phtml

In my module.config.php there is configuration:

'template_path_stack' => array(
            'application' => __DIR__ . '/../view',
        ),

And error:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "email/template.phtml"; resolver could not resolve to a file

where mistake ?

thanks

0
votes

I had the same problem after I added a view_manager config to the console config. The solution is to just pull the renderer from the ViewManager:

$renderer = $serviceLocator->get('ViewManager')->getRenderer();

This will also register the ViewRenderer service, so any calls to get('ViewRenderer') after this should work too.

0
votes

in ZF3 also a TemplateMapResolver can help you to fix some issues like: "PhpRenderer::render: Unable to render template"

$resolver = new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap([
    'mailTemplate' => __DIR__ . '/../../view/email/testmail.phtml'
]);
$renderer->setResolver($resolver);
$viewContent = new \Zend\View\Model\ViewModel($contentArray);
$viewContent->setTemplate('mailTemplate');