2012-05-18 5 views
0

내 zf2 컨트롤러에 다음 코드를 가지고 :설정 컨트롤러 constructer에서 일부 변수보기

<?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! 
} 

당신은 내가 젠드 \ 번역기 모듈을 사용하고 볼 수 있듯이.
내 컨트롤러 생성자의보기에 추가하고 싶습니다. 이미 시도 :

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

하지만 작동하지 않습니다.

도와주세요.

+0

이 정말 \ 젠드 \ 레지스트리를 사용하는 어떤 이유가 안 최종 솔루션 - 기본적으로 글로벌 변수는 당신이 '잘못되고있는 레지스트리에 넣어 객체 좋은 양식. 레지스트리를 사용하는 대신 DI를 사용하여 Translator를 구성하고 필요할 때마다 주입하십시오. – superdweebie

+0

예 ... 답글을 게시했습니다 –

답변

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') 
      ) 
     ), 
2

개인 클래스 변수 private $viewModel을 추가하십시오. 그런 다음 어떤 변수를 추가, 당신의 생성자에 뷰 모델을 만들 :

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

그런 다음 액션 함수에서 $this->viewModel을 반환합니다.

+0

감사합니다 plz 보세요이 문제는 https://github.com/doctrine/DoctrineORMModule/issues/37 –