简体   繁体   English

如何在Zend Framework 2中使用插件呈现自定义视图

[英]How to Render a custom view with plugins in Zend Framework 2

I'm working on an app that needs to send an email after a process is complete. 我正在开发一个需要在流程完成后发送电子邮件的应用。 Since the email needs to be HTML I had the bright idea of rendering a view as the email message body so that I can implement a "Click here to see this on your browser" functionality. 由于电子邮件需要是HTML,我有一个明智的想法,即将视图呈现为电子邮件消息正文,以便我可以实现“单击此处在浏览器上查看此内容”功能。 This is all taking part inside a controller that implements AbstractRestfulController so the view itself resides in my front end module so that it can be accessed from a URL through a browser. 这都是参与实现AbstractRestfulController的控制器,因此视图本身驻留在我的前端模块中,以便可以通过浏览器从URL访问它。 However I am getting an 但是我得到了一个

No RouteStackInterface instance provided 没有提供RouteStackInterface实例

error when I try to render the view. 我尝试渲染视图时出错。

This is my code: 这是我的代码:

use Zend\View\HelperPluginManager;
use Zend\View\Resolver;
use Zend\View\Renderer\PhpRenderer;

//Instantiate the renderer
$renderer = new PhpRenderer();
$resolver = new Resolver\TemplateMapResolver(array(
            'layout' => realpath(__DIR__ . '/../../../../Application/view/layout/email.layout.phtml'),
            'email/view' => realpath(__DIR__ . '/../../../../Application/view/application/email/view.phtml')
        )
    );
$renderer->setResolver($resolver);
$renderer->url()->setRouter($this->getEvent()->getRouter());

I saw on the API documentation that you can set the router to the URL plugin by giving it a RouteStackInterface, hence the last line. 我在API文档中看到,您可以通过为路由器提供RouteStackInterface将路由器设置为URL插件,因此最后一行。 However, that didn't seem to work either. 但是,这似乎也没有用。

I would like to use the same view to send an HTML email message that has links in the message body & to display on the browser through a URL. 我想使用相同的视图发送HTML邮件消息,该消息在邮件正文中有链接并通过URL显示在浏览器上。

Any ideas/suggestions as to how to accomplish this? 有关如何完成此任务的任何想法/建议?

EDIT/SOLUTION: 编辑/解决方案:

As per dotwired's answer below, getting the instance of the renderer from the service manager causes the plugins to be instantiated correctly. 根据下面的dotwired的回答,从服务管理器获取渲染器的实例会导致插件正确实例化。 So this is the code that worked: 所以这是有效的代码:

module.config.php: module.config.php:

array('view_manager' => array(
            'template_map' => array(
                    'layout/email'            => __DIR__ . '/../../Application/view/layout/email.layout.phtml',
                    'email/share'             => __DIR__ . '/../../Application/view/application/email/share.phtml',
                    'email/view'              => __DIR__ . '/../../Application/view/application/email/view.phtml',
            ),
    ),
);

REST controller: REST控制器:

use Zend\View\Model\ViewModel;

//get the renderer
    $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\RendererInterface');

    //Create the views
    $shareView = new ViewModel(array('data' => $data));
    $shareView->setTemplate('email/view');
    $emailLayout = new ViewModel(array('subject' => $this->_subject, 'content' => $renderer->render($shareView)));
    $emailLayout->setTemplate('layout/email');

    //Render the message
    $markup = $renderer->render($emailLayout);

Using the renderer from the service manager the $this->url() view helper work without issue. 使用服务管理器中的渲染器,$ this-> url()视图助手可以正常工作。

Just use the module.config.php of your module to specify your email template, like: 只需使用模块的module.config.php指定您的电子邮件模板,例如:

'view_manager' => array(
    'template_path_stack' => array(
        'user' => __DIR__ . '/../view'
    ),
    'template_map' => array(
        'email/view' => __DIR__ . '/../view/application/email/view.phtml'
    )
),

After which you can go on with this part of the documentation. 之后,您可以继续阅读这部分文档。 You can then pass your view template from the renderer to the MimePart which will be used by the MimeMessage like 然后,您可以将视图模板从渲染器传递到MimePart,MimeMessage将使用它

$viewModel = new \Zend\View\Model\ViewModel();
$viewModel->setTemplate('email/view');

$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$htmlPart = new \Zend\Mime\Part($renderer->render($viewModel));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM