簡體   English   中英

從視圖調用控制器-Zend Framework

[英]Calling a controller from a view - Zend Framework

如何從視圖中呈現控制器的結果?

這是我當前正在執行的操作,但是由於直接調用了視圖,因此...不會從我的控制器中獲取模型。 這是愚蠢的。

public function ListAction()
{
    $sm = $this->getServiceLocator();
    $this->userRepository = $sm->get('Application\Model\Concrete\TGUserRepository');

    $users = $this->userRepository->Users();

    $view = new ViewModel( array( "Users" => $users ) );

        $secondarySidebarView = new ViewModel(); // I don't really wanna do this, because I've already got an awesome controller set up for this which may as well be used to generate the model :S ?
        $secondarySidebarView->setTemplate('something/poo'); // This only gets the view and doesn't call the controller first :S

    $view->addChild($secondarySidebarView, 'something');
    return $view;
}

視圖:

<?php    
    foreach( $this->Users as $user )
    {
        echo $user->Email;
    }

    echo $this->something; // Works fine
    print_r( $this->something->Poos ); // Crashes because the controller hasn't been called to generate the Poos :(

我想在視圖中執行的操作是這樣的:

echo RenderFromController( "action", "controllername" );

這是一種實現此目的的方法:

public function listAction()
{

     $view = new ViewModel();
     $view->setTemplate('users.phtml');
     $poos = $this->forward()
                   ->dispatch('Foo\Controller', array('action' => 'action-name'));
     $view->addChild($poos, 'Poos');

     /* Add more variables to $view as needed */

     return $view;
}

users.phtml

<?php

    echo $this->poos; //Will output the result of Foo\Controller::action-name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM