Symfony: trying to retrieve a variable saved using

2019-08-07 20: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

标签: symfony1
1条回答
萌系小妹纸
2楼-- · 2019-08-07 20:44

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.

查看更多
登录 后发表回答