0

我正在尝试在 ZF2 中创建一个简单的电子邮件模板测试,我正在使用依赖注入来创建 PhpRenderer 类的实例,并设置了所有依赖项。

由于 AggregateResolver 中不存在路径“电子邮件”,看来我可能正在努力链接注入。

在 module.config.php 里面

'di' => array(
    'instance' => array(
        'Zend\View\Resolver\TemplatePathStack' => array(
            'options' => array(
                'script_paths'  => array(
                    'email' => __DIR__ . '/../view/application/email',
                ),
             ),
        ),
        'Zend\View\Resolver\AggregateResolver' => array(
            'attach' => array(
                'Zend\View\Resolver\TemplatePathStack',
            ),
        ),
        'Zend\View\Renderer\PhpRenderer' => array(
            'setResolver' => 'Zend\View\Resolver\AggregateResolver',
        ),
    ),
),

在 TestController.php 里面

    $di = new \Zend\Di\Di;
    $renderer = $di->get('Zend\View\Renderer\PhpRenderer');
    $content = $renderer->render('email/test', null);

    echo($content);

信息:

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

任何帮助将不胜感激。


由于编写了上述内容,我一直在玩耍并从 di 数组中删除了 TemplatePathStack,这根本没有任何效果,所以我不确定 AggregateResolver 是否正在使用它,所以它可能是一个链接问题:

'di' => array(
    'instance' => array(
        /*'Zend\View\Resolver\TemplatePathStack' => array(
            'addPaths' => array(
                'paths'  => array(
                    'email' => __DIR__ . '/../view/application/email',
                ),
             ),
        ),*/
        'Zend\View\Resolver\AggregateResolver' => array(
            'attach' => array(
                'Zend\View\Resolver\TemplatePathStack',
            ),
        ),
        'Zend\View\Renderer\PhpRenderer' => array(
            'setResolver' => 'Zend\View\Resolver\AggregateResolver',
        ),
    ),
),

阿博格罗夫

4

1 回答 1

1

最后,我通过将 di 数组移出 module.conf.php 并在 Module getServiceConf() 方法中重新创建它来解决它。(虽然我不确定这是放置它的最佳位置)

 public function getServiceConfig()
{
    return array(
        'factories' => array(
            // using the Session Manger to create one instance of the follwing models
            ......... more code here ............
            'EmailTemplatePathStack' => function($sm) {
                $template_path_stack =  new \Zend\View\Resolver\TemplatePathStack();
                $paths  = array('emailfolder' => __DIR__ . '/view/application/email');
                $template_path_stack->addPaths($paths);
                return $template_path_stack;

            },
            'EmailAggregateResolver' => function($sm) {
                $resolver =  new \Zend\View\Resolver\AggregateResolver();
                $resolver->attach($sm->get('EmailTemplatePathStack'));
                var_dump($resolver);
                return $resolver;

            },
            'EmailPhpRenderer' => function($sm) {
                $php_renderer =  new \Zend\View\Renderer\PhpRenderer();
                $php_renderer->setResolver($sm->get('EmailAggregateResolver'));
                return $php_renderer;
            },
        ),
    );
}

然后将控制器更改为:

    $sm = \Application\Model\ServiceLocatorFactory::getInstance();
    $renderer = $sm->get('EmailPhpRenderer');
    $content = $renderer->render('test.phtml', null);

    echo($content);
于 2013-05-31T13:09:09.083 回答