我的电脑有问题。我有一个邮件服务工厂。包含以下代码的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);
}
}
和所有的函数工作,但当我执行供应商/bin/doctirne-模块
我得到下面的错误:
致命错误:未捕获异常“Zend\ServiceManager\exception\ServiceNotFoundException”,消息“Zend\ServiceManager\ServiceManager::get无法在/Users/Daniele/Apps/corradini中获取或创建ViewRenderer的实例”。com/www/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager。菲律宾比索:529
为什么我得到这个错误?
您无法在控制台请求上直接从ServiceLocator
获取ViewRenderer
,因为请求不是HttpRequest
,所以它不在阶段中。
您可以轻松地尝试将一个PhpRenader
实例传递到您的服务,并使用它呈现一个ViewModel
,而不是ViewRenader
。例如:
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);
}
现在在您的邮件服务中:
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
}
希望能有帮助。
感谢愚蠢现在学说控制台功能!但是我不能读取呈现邮件的模板。
My tempate is locatend in module/Application/view/email/template.phtml
在我的模块中。配置。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
哪里出错了?
谢啦
在将视图管理器配置添加到控制台配置后,我遇到了同样的问题。解决方案是仅从ViewManager中拉出渲染器:
$renderer = $serviceLocator->get('ViewManager')->getRenderer();
这还将注册ViewRenderer服务,因此在此之后对get('ViewRenderer')
的任何调用都应该可以工作。