在symfony中将发布的值从一个控制器传递到另一个控制器

时间:2014-10-28 11:38:38

标签: php symfony

我有两个控制器。我发现将发布值从一个控制器传递到另一个控制器时出现问这是一个快速浏览,

这是函数1

public function setRole(request $request){

this->forward(Path,array(role=>$role));

this->redirect(path of second controller);

}

这是功能2。

public function getRole(request $request){

$role = $request->get('role');//when printing this $role, I am able to get the value of $role.

$sql = "select * from table where id=$role"; // I cannot get the value in this qry ,also, i cannot pass the value to a twig file

return render...(filename,array('roleid'=>$role));

}

问题是我无法在第二个控制器的twig文件中访问变量“roleid”。它总是空着。

我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您错过了Documentation

public function indexAction($name) {
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
    'role'  => $role
));

// ... further modify the response or return it directly. 
// But do not redirect afterwards! 
// Just return the response that the forwarded controller returns

return $response;
}

答案 1 :(得分:0)

如果其他人从谷歌搜索中找到了这个。从Symfony 3.3中,您可以使用session interface将事物从一个控制器传递到另一个控制器。

正如documentation所说:要检索会话,请将SessionInterface类型提示添加到您的参数中,Symfony将为您提供会话。

use Symfony\Component\HttpFoundation\Session\SessionInterface;

public function indexAction(SessionInterface $session)
{
    // store an attribute for reuse during a later user request
    $session->set('foo', 'bar');

    // get the attribute set by another controller in another request
    $foobar = $session->get('foobar');

    // use a default value if the attribute doesn't exist
    $filters = $session->get('filters', array());
}
相关问题