2017-08-09 79 views
1

在我的控制器動作,我想虎視眈眈的另一個動作呈現的整頁:ZF3:如何從控制器中的其他操作獲取操作的視圖?

class MycontrollerController extends AbstractActionController 
{ 
    public function firstactionAction() 
    { 
     $html = some_function_to_get_the_rendered_page_of_secondaction(); 
    } 

    public function secondactionAction() 
    { 
     return new ViewModel(); 
    } 
} 

回答

2

小心使用setTemplate()

MycontrollerControllerFactory.php

<?php 

namespace Application\Controller\Service; 

use Application\Controller\MycontrollerController; 
use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 

class MycontrollerControllerFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     $controller = new MycontrollerController(); 
     $controller->setRenderer($container->get('Zend\View\Renderer\PhpRenderer')); 
     return $controller; 
    } 
} 

MycontrollerController.php

<?php 

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

class MycontrollerController extends AbstractActionController 
{ 
    /** 
    * @var \Zend\View\Renderer\PhpRenderer 
    */ 
    protected $renderer; 

    /** 
    * @return \Zend\View\Renderer\PhpRenderer 
    */ 
    public function getRenderer() 
    { 
     return $this->renderer; 
    } 

    /** 
    * @param \Zend\View\Renderer\PhpRenderer $renderer 
    * @return self 
    */ 
    public function setRenderer($renderer) 
    { 
     $this->renderer = $renderer; 
     return $this; 
    } 

    public function firstAction() 
    { 
     if ($this->yourMethod()) { 
      $secondView = $this->secondAction(); 
      $html = $this->getRenderer()->render($secondView); 
     } 

     $view = new ViewModel(); 
     $view->setTemplate('namespace/my-controller/first'); 
     return $view; 
    } 

    public function secondAction() 
    { 
     $view = new ViewModel(); 
     $view->setTemplate('namespace/my-controller/second'); 
     return $view; 
    } 
} 
+0

這很有用,但我仍然需要更多的幫助。假設我做了這個'$ otherViewModel = $ this-> secondAction();',現在我想獲取該視圖模型的呈現html,例如'$ html = $ otherViewModel-> some_render_function()',怎麼做那? – evilReiko

+0

我修改了我的答案;-) –

+0

OMG這是完美的! :D – evilReiko

1

所以,我建議創建一個新的插件 'htmlRender':

module.config.php

'controller_plugins' => [ 
    'factories' => [ 
     'htmlRender' => Application\Mvc\Controller\Plugin\Service\HtmlRenderFactory::class, 
    ], 
], 

HtmlRenderFactory.php

<?php 
namespace Application\Mvc\Controller\Plugin\Service; 

use Application\Mvc\Controller\Plugin\HtmlRender; 
use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 
use Zend\View\Renderer\PhpRenderer; 

class HtmlRenderFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     $plugin = new HtmlRender(); 
     $plugin->setRenderer($container->get(PhpRenderer::class)); 
     return $plugin; 
    } 
} 

HtmlRender.php

<?php 
namespace Application\Mvc\Controller\Plugin; 

use Zend\Mvc\Controller\Plugin\AbstractPlugin; 
use Zend\View\Renderer\RendererInterface; 

class HtmlRender extends AbstractPlugin 
{ 
    /** 
    * @var \Zend\View\Renderer\PhpRenderer 
    */ 
    protected $renderer; 

    /** 
    * @param string|\Zend\View\Model\ModelInterface $nameOrModel 
    * @param null|array|\Traversable $values 
    * @param string|bool|\Zend\View\Model\ModelInterface $layout 
    * @return string 
    */ 
    public function __invoke($nameOrModel, $values = null, $layout = false) 
    { 
     $content = $this->getRenderer()->render($nameOrModel, $values); 
     if (!$layout) { 
      return $content; 
     } 

     if (true === $layout) { 
      $layout = 'layout/layout'; 
     } 
     return $this->getRenderer()->render($layout, [ 
      'content' => $content, 
     ]); 
    } 

    /** 
    * @return \Zend\View\Renderer\PhpRenderer 
    */ 
    public function getRenderer() 
    { 
     return $this->renderer; 
    } 

    /** 
    * @param \Zend\View\Renderer\PhpRenderer|RendererInterface $renderer 
    * @return self 
    */ 
    public function setRenderer(RendererInterface $renderer) 
    { 
     $this->renderer = $renderer; 
     return $this; 
    } 
} 

在Mycontrolle中使用rController.php

<?php 
class MycontrollerController extends AbstractActionController 
{ 
    public function firstAction() 
    { 
     if ($this->yourMethod()) { 
      // Option 1 without layout 
      $html = $this->htmlRender($secondView); 
      // Option 2 without layout 
      $html = $this->htmlRender('namespace/my-controller/second', $yourVariables)); 

      // Option 1 with layout 
      $html = $this->htmlRender($secondView, null, true); 
      //$html = $this->htmlRender($secondView, null, 'layout/my-custom-layout'); 
      // Option 2 with layout 
      $html = $this->htmlRender('namespace/my-controller/second', $yourVariables, true)); 
      //$html = $this->htmlRender('namespace/my-controller/second', $yourVariables, 'layout/my-custom-layout'); 
     } 

     $view = new ViewModel(); 
     $view->setTemplate('namespace/my-controller/first'); 
     return $view; 
    } 

    public function secondAction() 
    { 
     $view = new ViewModel(); 
     $view->setTemplate('namespace/my-controller/second'); 
     return $view; 
    } 
} 
+0

太好了,你的答案實際上包含了我一直在想的幾個答案!自定義插件的使用可能是一個好主意,但在我的情況下,我想知道的(我剛發佈的)是如何使用phpRenderer。非常感謝。我會保留你的第一個答案作爲正確的答案,因爲它回答了原來的問題 – evilReiko

相關問題