[英]Assigning variables during render event to phprenderer not passing to layout
i am trying to assign variables to my view renderer during an event i attached to the render event
and its not producing values. 我试图在附加到render event
的事件中为其分配变量给我的视图渲染器,并且不产生值。
I have an event that i attached to the render event
in my Application Module
. 我有一个附加到我的Application Module
的render event
。 I also have the method which gets called during the render event operation
This is a Zend Framework 3 Application however I tagged Zend Framework 2 for more exposure since the framework is very new. 我还具有在render event operation
期间调用的方法。 这是一个Zend Framework 3应用程序,但是由于该框架是非常新的,因此我对Zend Framework 2进行了标记,以进行更多了解。 Here it is 这里是
namespace Application;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach('render', array($this, 'setAssignRouteVariables'));
}
public function setAssignRouteVariables(MvcEvent $e)
{
$matches = $e->getRouteMatch();
$action = $matches->getParam('action');
$controller = $matches->getParam('controller');
$designHandler = $e->getApplication()->getServiceManager()->get('DesignHandler');
$designHandler->getPhprenderer()->controllerName='testcontroller';
$designHandler->getPhprenderer()->actionName='testaction';
}
}
If you look, I am calling the method getPhprenderer()
using the DesignHandler
class. 如果您看,我正在使用DesignHandler
类调用方法getPhprenderer()
。 This method returns an instance of \\Zend\\View\\Renderer\\PhpRenderer
. 此方法返回\\Zend\\View\\Renderer\\PhpRenderer
。 In any case after this i assign variables to the renderer object. 在此之后的任何情况下,我都将变量分配给渲染器对象。
Following this, in my layout - application/view/layout/layout.phtml
i do this: 在此之后, in my layout - application/view/layout/layout.phtml
我这样做:
<script type="text/javascript">
var currentController = '<?php echo $this->controllerName;?>';
var currentAction = '<?php echo $this->actionName;?>';
</script>
I'm getting blank values
in this. 我在此获得blank values
。
What am I doing incorrectly here? 我在这里做错了什么?
Here's the current result : 这是当前结果 :
<script type="text/javascript">
var currentController = '';
var currentAction = '';
</script>
This worked. 这工作了。
public function setAssignRouteVariables(MvcEvent $e)
{
$matches = $e->getRouteMatch();
$action = $matches->getParam('action');
$controller = $matches->getParam('controller');
$e->getViewModel()->controllerName=$controller;
$e->getViewModel()->actionName=$action;
}
You can use $GLOBALS global variable instead. 您可以改用$ GLOBALS全局变量。 I faced same problem but did not find better solution. 我遇到了同样的问题,但是没有找到更好的解决方案。
$GLOBALS["my_index"] = "My value";
in javascript 在JavaScript中
var myval = '<?=$GLOBALS["my_index"]?>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.