Symfony - 同时发送带和不带URL的参数

Symfony - 同时发送带和不带URL的参数

问题描述:

我想同时发送URL中的一些参数(使用redirectToRoute),并且一些参数不在URL中(使用render)。我能怎么做 ?Symfony - 同时发送带和不带URL的参数

要显示一个例子:我有两个变种:A和B

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

你能告诉我一个代码的例子吗?

由于

+0

或使用这种方法https://*.com/questions/11227975/symfony-2-redirect-using-post/31031986#31031986 – LBA

尼斯和容易,只需通过键/值的阵列到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

甲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'); 
    // ... 
}