2017-09-25 69 views
1

我想同時發送URL中的一些參數(使用redirectToRoute),並且一些參數不在URL中(使用render)。我能怎麼做 ?Symfony - 同時發送帶和不帶URL的參數

要顯示一個例子:我有兩個變種:A和B

A需要在網址:http://website.com?A=smth B需要被髮送到完成樹枝(但不是以URL)

你能告訴我一個代碼的例子嗎?

由於

+0

或使用這種方法https://stackoverflow.com/questions/11227975/symfony-2-redirect-using-post/31031986#31031986 – LBA

回答

0

尼斯和容易,只需通過鍵/值的陣列到render()方法:

$template = $twig->load('index.html'); 
echo $template->render(array('the' => 'variables', 'go' => 'here')); 

https://twig.symfony.com/doc/2.x/api.html#rendering-templates

+0

我不認爲@Destunk想通過變量來呈現,但通過重定向存儲/檢索它們,因爲在問題中提到了'redirectToRoute'。 – nifr

1

甲HTTP 3XX重定向並NOT具有主體,以便您不能同時通過render包含數據並使用redirectToRoute('redirect_target_route', array('A' => 'smth'})

您需要將數據保存在會議Flashbag中,並從redirect_target_route的控制器操作內部獲取該數據。

public function redirectingAction(Request $request) 
{ 
    // ... 
    // store the variable in the flashbag named 'parameters' with key 'B' 
    $request->getSession()->getFlashBag('parameters')->add('B', 'smth_else'); 

    // redirect to uri?A=smth 
    return $this->redirectToRoute('redirect_target_route', array('A' => 'smth'}); 
} 

public function redirectTargetAction(Request $request) 
{ 
    $parameterB = $request->getSession()->getFlashBag('parameters')->get('B'); 
    // ... 
}