すべてのビューで使用されるレイアウトがあり、コントローラーからこのレイアウトに変数を割り当てる必要があります。コントローラーでこのメソッドを使用すると、機能しません。
public function indexAction()
{
return new ViewModel( array(
'testvar' => 'bla',
));
}
誰でも私を助けることができますか?
ありがとう
すべてのビューで使用されるレイアウトがあり、コントローラーからこのレイアウトに変数を割り当てる必要があります。コントローラーでこのメソッドを使用すると、機能しません。
public function indexAction()
{
return new ViewModel( array(
'testvar' => 'bla',
));
}
誰でも私を助けることができますか?
ありがとう
ZF2(コントローラー内)でこれを実現するには、次の3つの方法があります。
初め:
$this->layout()->someVariableName = 'Some value for the variable';
2番:
$this->layout()->setVariable('someVariableName', 'Some value for the variable');
第3:
$this->layout()->setVariables(array(
'someVariableName' => 'Some value for the variable',
'anotherVariable' => 'Some value for another variable',
);
Rob Allenが、別のビューモデル(例:レイアウト)でビュー変数にアクセスする方法についてのすばらしい記事を投稿しました。
基本的に、layout.phtml内に配置された次のコードは、ニーズに一致します。
<?php
$children = $this->viewModel()->getCurrent()->getChildren();
$child = $children[0];
?>
<!-- some HTML -->
<?php echo $this->escape($child->myvar);?>
やってみました:
$this->layout()->testvar = 'bla';
コントローラプラグインを使用するlayout
と、で使用されているViewModelオブジェクトを取得できますlayout.phtml
。
ZF2 ViewModelはツリー構造であるため、レイアウトは実際にはViewModelのルートノードであり、コントローラーのViewModelはレイアウトの子ノードとして追加されます。
MvcEventにアクセスしてレイアウトViewModelにアクセスできます。コントローラーでこれを試してください。
public function indexAction()
{
$events = $this->getServiceLocator()->get('Application')->getEventManager();
$events->attach(MvcEvent::EVENT_RENDER, array($this, 'setVariableToLayout'), 100);
}
public function setVariableToLayout($event)
{
$viewModel = $this->getEvent()->getViewModel();
$viewModel->setVariables(array(
'testvar' => 'bla',
));
}
以下の「ビュー変数の追加」セクションを参照してください。
module.phpファイルに追加します。
ビューヘルパーを使用してこれを行うこともできます。
/**
* Remember to keep the init() method as lightweight as possible
*
* @param \Zend\ModuleManager\ModuleManager $moduleManager
*/
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
$events->attach('loadModules.post', array($this, 'modulesLoaded'));
$events->attach('onBootstrap', array($this, 'bootstrap'));
$sharedEvents = $events->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'bootstrap', array($this, 'bootstrap'), 100);
$sharedEvents->attach(__NAMESPACE__, 'bootstrap', array($this, 'initializeView'), 100);
$sharedEvents->attach(__NAMESPACE__, 'dispatch', array($this, 'addViewVariables'), 201);
}
/**
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function loadConfiguration(MvcEvent $e)
{
$e->getApplication()->getServiceManager()
->get('ControllerPluginManager')->get('AclPlugin')
->checkAcl($e); //Auth/src/Auth/Controller/AclPlugin
}
/**
*
* @param \Zend\EventManager\EventInterface $e
*/
public function bootstrap(Event $e) {
$eventManager = $e->getParam('application')->getEventManager();
//$app->getEventManager()->attach('dispatch', array($this, 'checkAcl'), 100);
}
/**
* pass variables to layout
*
* @param \Zend\EventManager\EventInterface $e
*/
public function addViewVariables(Event $e)
{
$route = $e->getRouteMatch();
$viewModel = $e->getViewModel();
$variables = $viewModel->getVariables();
if (false === isset($variables['controller'])) {
$viewModel->setVariable('controller', $route->getParam('controller'));
}
if (false === isset($variables['action'])) {
$viewModel->setVariable('action', $route->getParam('action'));
}
$viewModel->setVariable('module', strtolower(__NAMESPACE__));
}
/**
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function initializeView(Event $e)
{
}
レイアウトでは、上記の例に従って、$ module、$ action、$controllerなどの名前を使用してこれらの変数に簡単にアクセスできます。
レイアウトにグローバルに値を渡したい場合は、これを参照できます:https ://stackoverflow.com/a/21455737/2190889