ZF2如何在布局中设置搜索表单

时间:2012-12-10 16:00:24

标签: forms layout zend-framework2

在layout.ptml中使用Zend Framework 2设置(搜索)表单的正确方法是什么?谁在网站的任何页面上都可见?

提前致谢。

尼克

2 个答案:

答案 0 :(得分:4)

通过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,
        ));
    }
}

答案 1 :(得分:0)

对于布局使用中的视图:

enter code here
    if ($user = $this->identity()) {
        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 {

        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();
    }