如何在 Zend Framework 2 中使用插件呈现自定义视图 [英] How to Render a custom view with plugins in Zend Framework 2

查看:36
本文介绍了如何在 Zend Framework 2 中使用插件呈现自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在流程完成后发送电子邮件的应用.由于电子邮件需要是 HTML,我有一个聪明的想法,将视图呈现为电子邮件正文,以便我可以实现单击此处在浏览器上查看"功能.这一切都在实现 AbstractRestfulController 的控制器中参与,因此视图本身驻留在我的前端模块中,以便可以通过浏览器从 URL 访问它.但是我得到了一个

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. 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. However I am getting an

未提供 RouteStackInterface 实例

No RouteStackInterface instance provided

当我尝试渲染视图时出错.

error when I try to render the view.

这是我的代码:

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

我在 API 文档中看到,您可以通过给它一个 RouteStackInterface 来将路由器设置为 URL 插件,因此是最后一行.然而,这似乎也不起作用.

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. However, that didn't seem to work either.

我想使用相同的视图发送一封 HTML 电子邮件,邮件正文中包含链接 &通过 URL 在浏览器上显示.

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.

有关如何实现这一目标的任何想法/建议?

Any ideas/suggestions as to how to accomplish this?

编辑/解决方案:

根据下面 dotwired 的回答,从服务管理器获取渲染器的实例会导致插件被正确实例化.所以这是有效的代码:

As per dotwired's answer below, getting the instance of the renderer from the service manager causes the plugins to be instantiated correctly. 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 控制器:

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

使用来自服务管理器的渲染器,$this->url() 视图助手可以正常工作.

Using the renderer from the service manager the $this->url() view helper work without issue.

推荐答案

只需使用您模块的 module.config.php 来指定您的电子邮件模板,例如:

Just use the module.config.php of your module to specify your email template, like:

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

之后你可以继续这部分 的文档.然后,您可以将视图模板从渲染器传递给 MimePart 将被 MimeMessage 使用,如

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

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

这篇关于如何在 Zend Framework 2 中使用插件呈现自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆