Legen Sie eine Ansichtsvariable im Controller-Konstrukter fest
-
11-12-2019 - |
Frage
Ich habe den folgenden Code in meinem ZF2-Controller:
<?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!
}
Wie Sie sehen, verwende ich das Modul zend ranslator.
Ich möchte es der Ansicht in meinem Controller-Konstruktor hinzufügen.Ich habe es schon versucht:
return ViewModel(array('tr'=>$translator));
Aber das funktioniert nicht.
Bitte helfen Sie.
Lösung 2
endgültiges lösung module.config.php generasacodicetagpre.
Andere Tipps
Fügen Sie eine private Klassenvariable hinzu private $viewModel
.Erstellen Sie dann das ViewModel in Ihrem Konstruktor und fügen Sie alle Variablen hinzu:
$this->viewModel = new ViewModel();
$this->viewModel->tr = $translator;
Dann kehre zurück $this->viewModel
aus Ihrer Aktionsfunktion.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow