Symfony: trying to retrieve a variable saved using sfContext::getInstance()

穿精又带淫゛_ 提交于 2020-01-05 07:08:13

问题


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');

}

When i execute module1/method1 i get this error:

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

Any idea?

Javi


回答1:


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.

There are three options here:

  • 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.

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



来源:https://stackoverflow.com/questions/2896274/symfony-trying-to-retrieve-a-variable-saved-using-sfcontextgetinstance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!