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

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

问题描述:

我有两个控制器。我发现从一个控制器传递值到另一个控制器的问题。这里是一个快速视图,在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)); 

} 

问题是我could'n在我的树枝访问变量“角色ID”第二个控制器的文件。总是变得空虚。

有什么我错过了吗?

+1

http://symfony.com/doc/current/book/controller.html#forwarding – zizoujab 2014-10-28 12:10:13

你已经错过了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; 
} 
+0

它现在有效。 TX。 – Ranjan 2014-10-28 13:29:52

万一别人从谷歌搜索发现这一点。从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()); 
}