Symfony:尝试检索使用 sfContext::getInstance() 保存的变量 [英] Symfony: trying to retrieve a variable saved using sfContext::getInstance()

查看:25
本文介绍了Symfony:尝试检索使用 sfContext::getInstance() 保存的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 module1/actions/actions.class.php 中有这些方法:

i have these methods in module1/actions/actions.class.php:

public function executeMethod1(sfWebRequest $request){

  $a = 10;

  sfContext::getInstance()->set('a', $a);
  return $this->redirect('module1/method2');

}

public function executeMethod2(sfWebRequest $request){

  echo sfContext::getInstance()->get('a');

}

当我执行 module1/method1 时出现这个错误:

When i execute module1/method1 i get this error:

当前上下文中不存在a"对象."

"The "a" object does not exist in the current context."

有什么想法吗?

贾维

推荐答案

重定向告诉浏览器加载另一个页面,该页面终止当前操作并产生具有新上下文的新请求.

The redirect is telling the browser to load another page which terminates the current action and results in a new request that has a new context.

这里有三个选项:

  • 您可以使用 转发 如果您希望 module1/method2 作为第一个请求的结果被执行.
  • 您可以使用 flash 属性$a 传递给下一个请求.
  • 您可以使用 会话属性 如果 $a 必须在下一个请求之后存活.
  • You could use a forward if you want module1/method2 to be executed as the result of the first request.
  • You could use a flash attribute to pass $a to the next request.
  • You could use a session attribute if $a has to live beyond the next request.

您也不需要在重定向之前使用 return 语句.

You don't need the return statement before the redirect either.

这篇关于Symfony:尝试检索使用 sfContext::getInstance() 保存的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆