2014-10-28 3 views
0

У меня есть два контроллера. Я нахожу проблему в передаче отправленных значений с одного контроллера на другой. Вот быстрый взгляд,Передача опубликованных значений от одного контроллера к другому в 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 доступ к переменной «Идентификатор роли» в моей веточке файл второго контроллера. Всегда он пуст.

Есть ли что-нибудь, что я пропустил здесь?

+1

http://symfony.com/doc/current/book/controller.html#forwarding – zizoujab

ответ

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; 
} 
+0

Он работает в настоящее время. ТХ. – Ranjan

0

В случае, если кто-то считает, что это от Googling. Из 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()); 
} 
Смежные вопросы