0

我的 zf2 控制器中有以下代码:

<?php
namespace Accounting\Controller;

use Zend\Mvc\Controller\ActionController,
Zend\View\Model\ViewModel,
Accounting\Model,
Zend\Paginator,
Accounting\Scripts\CMSTranslator;

class AdminController extends ActionController {

protected $translator;

public function setTranslator(CMSTranslator $translator) {
    $this->translator = $translator;
    return $this;
}

public function __construct(\Doctrine\ORM\EntityManager $em,CMSTranslator $translator) {
    $this->em = $em;

    //$this->translator = new \Zend\Translator\Translator('ArrayAdapter', __DIR__ . '/../../../lang/lang-fa.php', 'fa');
    $this->translator = $translator;

    \Zend\Registry::set('tr', $this->translator);
    // now you can use the EntityManager!
}

如您所见,我正在使用 zend\translator 模块。
我想将它添加到我的控制器构造函数中的视图中。我已经尝试过:

return ViewModel(array('tr'=>$translator));

但这不起作用。

请帮忙。

4

2 回答 2

2

添加一个私有类变量private $viewModel。然后在您的构造函数中创建 ViewModel,添加任何变量:

$this->viewModel = new ViewModel();
$this->viewModel->tr = $translator;

然后$this->viewModel从您的操作函数返回。

于 2012-05-19T02:51:00.213 回答
1

最终解决方案 module.config.php

'Accounting\Controller\AccountingController' => array(
            'parameters' => array(
                'em' => 'doctrine_em',
                'translator' => 'Accounting\Scripts\CMSTranslator',
            ),
        ),
        'Zend\View\Helper\Translator' => array(
            'parameters' => array(
                'translator' => 'Accounting\Scripts\CMSTranslator'
            )
        ),
        'Accounting\Scripts\CMSTranslator' => array(
            'parameters' => array(
                'options' => array('adapter' => 'ArrayAdapter', 'content' => __DIR__ . '/../lang/lang-fa.php', 'local' => 'fa')
            )
        ),
        'translateAdapter' => array(
            'parameters' => array(
                'options' => array('adapter' => 'ArrayAdapter', 'content' => __DIR__ . '/../lang/lang-fa.php', 'local' => 'fa')
            )
        ),
于 2012-05-19T08:08:20.497 回答