如何在Zend Framework 2中包含多个动作和控制器

问题描述:

I would like to include multiple views within a view in ZF2. I read this link: http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html

but there is a problem. In this way, I have to pass the values that are within a view, like this:

$secondView = new ViewModel (array('var1' => $var1 ......));

In this mode the controller and second action are bypassed. Is there a way to include a view without bypassing them? I would like variables to be passed from the second action controller, like an include php statement

我想在ZF2的视图中包含多个视图。 读取此链接: http://framework.zend.com/manual/current/ en / modules / zend.view.quick-start.html p>

但是有问题。 这样,我必须传递视图中的值,如下所示: p>

  $ secondView = new ViewModel(array('var1'=> $ var1。  .....)); 
  code>  pre> 
 
 

在此模式下,绕过控制器和第二个动作。 有没有办法在不绕过它们的情况下包含视图? 我想从第二个动作控制器传递变量,比如包含php语句 p> div>

If I understood correctly your question, I think that what you're asking is not possible.

My suggestion would be to move the retrieval of the data you need for the secondView somewhere else, away from the controller, and call it both from the second controller and form the first controller to pass them to the secondView.

If you really want to go on with your approach, the only possibility I see is to use javascript and ajax calls to retrieve the partials you need in your view

You can use partials for that.

In your module.config.php under the 'view_manager' key you define a template map for your partial:

'view_manager' => array(
    'template_map' => array(
        'myPartial'   => __DIR__ . '/../view/mymodule/partial/myPartial.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

Then in the view container you can use that partial using the partial() View Helper:

<div><?php echo $this->partial('myPartial', array('var1' => 'value1'); ?></div>

You can also pass variables to your partial. Those variables are referenced in your partial like any other view variable:

echo $var1;