2012-12-10 34 views

回答

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, 
     )); 
    } 
} 
+0

是的,很簡單!:)非常感謝! – directory

+0

如果'表單'不在layout.phtml中,但在某些部分中,則可以以'$ form = $ this-> layout() - >表單的形式進行訪問;' –

+0

是否可以擴展該表單在控制器?我的意思是在一些控制器中,我想添加一些輸入,在其他控制器中添加一些選擇等等。 – Maciej

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