Zend framework2 在其他模块ZF2中渲染部分

Zend framework2 在其他模块ZF2中渲染部分,zend-framework2,Zend Framework2,在我的项目中,我必须使用模块 模块1 模块2 在模块1中,我有一个视图需要渲染模块2中的视图,因此我要做的是: $this->partial('partials/hello/title.phtml','Module2',array('data' => $data)) 看起来我调用的视图是正确的,但是在view title.phtml中我无法使用数据 未定义变量:数据位于/site/src/module/Module2/view/partials/hello/title.phtm

在我的项目中,我必须使用模块

  • 模块1
  • 模块2
在模块1中,我有一个视图需要渲染模块2中的视图,因此我要做的是:

$this->partial('partials/hello/title.phtml','Module2',array('data' => $data))
看起来我调用的视图是正确的,但是在view title.phtml中我无法使用数据

未定义变量:数据位于/site/src/module/Module2/view/partials/hello/title.phtml中

是否需要添加与配置相关的内容


谢谢

您没有正确调用它,请尝试:

$this->partial('partials/hello/title.phtml', array('data' => $data));
见:


分部代码位于不同的模块中这一事实并不重要。必须将模块名指定为第二个参数仅在ZF1中使用。

如果调用不正确,请尝试:

$this->partial('partials/hello/title.phtml', array('data' => $data));
见:


分部代码位于不同的模块中这一事实并不重要。必须将模块名称指定为第二个参数仅在ZF1中使用。

另一个解决方案是在模块引导中添加您自己的视图路径解析器:

class Module
{
    /* @var \Zend\ServiceManager\ServiceManager $SM */
    public static $SM;
    /* @var \Zend\EventManager\EventManager $EM */
    public static $EM; 

    public function onBootstrap(MvcEvent $e)
    {
        self::$SM = $e->getApplication()->getServiceManager();
        self::$EM = $e->getApplication()->getEventManager();

        //change view resolver to resolve views from {Module}/view/{Controller}/{Action}.phtml path
        self::$EM->attach('dispatch', function($e) {
            self::$SM->get('ViewRenderer')->resolver()->attach(
                    new \Engine\View\Resolver\MCA() , 10
             );
        });
    }
    ....
解析程序获取字符串,必须返回path或false(如果未解析),只需编写解析程序即可理解其他模块路径:

<?php
namespace Engine\View\Resolver;

use Zend\View\Renderer\RendererInterface as Renderer;

/**
 * Resolves view scripts based on a stack of paths
 */
class MCA implements \Zend\View\Resolver\ResolverInterface
{
    public function resolve($name, Renderer $renderer = null)
    {
        $path = explode('/', $name);
        if (count($path)<3){
            return false;
        }
        $module = array_shift($path);
        $resolvedPath = ROOT_PATH . '/module/'. ucfirst($module) . '/view/' . implode('/', $path). '.phtml';
        if (!file_exists($resolvedPath)){
            return false;
        }
        return $resolvedPath;
    }
}

另一个解决方案是在模块引导中添加您自己的视图路径解析器:

class Module
{
    /* @var \Zend\ServiceManager\ServiceManager $SM */
    public static $SM;
    /* @var \Zend\EventManager\EventManager $EM */
    public static $EM; 

    public function onBootstrap(MvcEvent $e)
    {
        self::$SM = $e->getApplication()->getServiceManager();
        self::$EM = $e->getApplication()->getEventManager();

        //change view resolver to resolve views from {Module}/view/{Controller}/{Action}.phtml path
        self::$EM->attach('dispatch', function($e) {
            self::$SM->get('ViewRenderer')->resolver()->attach(
                    new \Engine\View\Resolver\MCA() , 10
             );
        });
    }
    ....
解析程序获取字符串,必须返回path或false(如果未解析),只需编写解析程序即可理解其他模块路径:

<?php
namespace Engine\View\Resolver;

use Zend\View\Renderer\RendererInterface as Renderer;

/**
 * Resolves view scripts based on a stack of paths
 */
class MCA implements \Zend\View\Resolver\ResolverInterface
{
    public function resolve($name, Renderer $renderer = null)
    {
        $path = explode('/', $name);
        if (count($path)<3){
            return false;
        }
        $module = array_shift($path);
        $resolvedPath = ROOT_PATH . '/module/'. ucfirst($module) . '/view/' . implode('/', $path). '.phtml';
        if (!file_exists($resolvedPath)){
            return false;
        }
        return $resolvedPath;
    }
}

我使用$data=$this->viewModel()->getCurrent()->data解决了这个问题;当使用来自其他模块的部分时,是否需要此功能?因为直到现在我还能够使用$data,因为我在同一个模块中使用了分部。我使用$data=$this->viewModel()->getCurrent()->data解决了这个问题;当使用来自其他模块的部分时,是否需要此功能?因为到目前为止,我能够使用$data,因为我在同一个模块中使用了partial