我的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));
但这不起作用。
请帮忙。
答案 0 :(得分:2)
添加私有类变量private $viewModel
。然后在construtor中创建ViewModel,添加任何变量:
$this->viewModel = new ViewModel();
$this->viewModel->tr = $translator;
然后从您的动作函数返回$this->viewModel
。
答案 1 :(得分: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')
)
),