dqrl3595 2016-04-06 09:12
浏览 317

如何从zf2中的Module.php发送数据进行查看

I searched for the same and got some answer about it, but did not help.

Those way I tried are as follows:

1)

in onBootstrap($e) function:

$e->getViewModel()->setVariable('test_variable', 'Hello World!');
        //or
$e->getViewModel()->test_variable = 'Hello World!';

2) I created a function in Module.php

function boforeDispatch(MvcEvent $event) {
    $event->getViewModel()->setVariable('test_variable', 'Hello World!');
    //or
    $event->getViewModel()->test_variable = 'Hello World!';
}

and called this function inside the onBootstrap($e) function

$application = $e->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'boforeDispatch'), 100);

when I tried in view

echo $test_variable;

it gives undefined variable test_variable in ... error all the times.

Any idea?

  • 写回答

1条回答 默认 最新

  • dongrao9454 2016-04-06 10:15
    关注

    Try using render event (MvcEvent::EVENT_RENDER) instead:

    public function onBootstrap($event){
        $application = $event->getApplication();
        $eventManager = $application->getEventManager();
        $events->attach(MvcEvent::EVENT_RENDER, array($this, 'addVariablesToViewModel'), 100);
    }
    
    public function addVariablesToViewModel($event)
    {
        $viewModel = $this->getEvent()->getViewModel();
        $viewModel->setVariables(array(
            'key' => 'value',
        ));
    }
    

    Your view model is probably not yet available on (pre) dispatch

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?