コントローラコンストラクタでView Variableを設定します
-
11-12-2019 - |
質問
私は私の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));
.
しかしそれはうまくいきません。
助けてください。
解決 2
Final Solution 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')
)
),
. 他のヒント
プライベートクラス変数private $viewModel
を追加します。その後、ContlueterでViewModelを作成し、変数を追加します。
$this->viewModel = new ViewModel();
$this->viewModel->tr = $translator;
.
はあなたのアクション関数から$this->viewModel
を返します。
所属していません StackOverflow