在网站的任何页面上可见的 layout.ptml 中使用 Zend Framework 2 设置(搜索)表单的正确方法是什么?
提前致谢。
缺口
在网站的任何页面上可见的 layout.ptml 中使用 Zend Framework 2 设置(搜索)表单的正确方法是什么?
提前致谢。
缺口
通过 EventManager 为 ZF2 中的所有布局设置任何变量非常简单,只需附加 EVENT_RENDER 事件,例如:
class Module
{
public function onBootstrap($e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this, 'setFormToView'), 100);
}
public function setFormToView($event)
{
$form = new MyForm();
$viewModel = $event->getViewModel();
$viewModel->setVariables(array(
'form' => $form,
));
}
}
对于布局中的视图使用:
<?php if ($user = $this->identity()): ?>
<?php echo 'Login with user' . $this->escapeHtml($user->nome); ?>
| <a href="<?php echo $this->url('auth/default', array('controller' => 'index', 'action' => 'logout'));?>"><?php echo $this->translate('Sair'); ?></a>
<?php else: ?>
<?php
echo $this->form()->openTag($form);
echo "<h5>Forneça seu login e senha </h5>";
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('rememberme'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
<?php endif; ?>