컨트롤러 생성자에서 일부 뷰 변수를 설정합니다.
-
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 ranslator 모듈을 사용하고 있습니다.
컨트롤러 생성자의 뷰에 추가하고 싶습니다.나는 이미 시도했다:
return ViewModel(array('tr'=>$translator));
하지만 그건 작동하지 않습니다.
도와주세요.
해결책 2
최종 솔루션 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
.그런 다음 생성자에서 ViewModel을 만들고 변수를 추가합니다.
$this->viewModel = new ViewModel();
$this->viewModel->tr = $translator;
그런 다음 돌아오세요 $this->viewModel
귀하의 작업 기능에서.
제휴하지 않습니다 StackOverflow