ZF2控制台错误 - Get无法为ViewRenderer获取或创建实例

时间:2014-11-08 11:18:04

标签: php doctrine-orm zend-framework2 command-line-interface

我遇到了doctrine控制台的问题。我有一个包含以下代码的MailServiceFactory.php:

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);
    }
}

并且所有函数都有效,但是当我执行vendor/bin/doctirne-module时,我收到以下错误:

  

致命错误:未捕获的异常   ' Zend的\的ServiceManager \异常\ ServiceNotFoundException的'与消息   ' Zend \ ServiceManager \ ServiceManager :: get无法获取或创建   ViewRenderer的一个实例'在   /Users/Daniele/Apps/corradini.com/www/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:529

为什么我收到此错误?

4 个答案:

答案 0 :(得分:0)

您无法直接从ViewRenderer获取ServiceLocator控制台请求,因为它不在阶段,因为请求不是HttpRequest

您可以轻松尝试将ViewRenderer实例传递给您的服务,并使用该实例呈现PhpRenderer,而不是ViewModel。例如:

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);
    }

现在在您的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
   }

希望它有所帮助。

答案 1 :(得分:0)

现在感谢foozy doctrine console funciton!但我无法读取渲染邮件的tempalte。

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

在我的module.config.php中有配置:

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

错误:

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

哪里出错?

感谢

答案 2 :(得分:0)

将view_manager配置添加到控制台配置后,我遇到了同样的问题。解决方案是从ViewManager中拉出渲染器:

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

这也将注册ViewRenderer服务,因此在此之后对get('ViewRenderer')的任何调用都应该有效。

答案 3 :(得分:0)

在ZF3中,TemplateMapResolver也可以帮助您解决一些问题,例如: “ PhpRenderer :: render:无法呈现模板”

$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');