简体   繁体   English

symfony2 GET参数覆盖路由参数

[英]symfony2 GET parameter overrides route parameter

I have an action which capable on several routes. 我有一个可以在多个路线上执行的动作。 I just defined the conflict part here. 我只是在这里定义了冲突部分。

/**
* @Route("/arama/ilanlar/{vehicleType}", requirements={"vehicleType"="(^$|araba|ticari-arac|motosiklet)"}, name="offer_search_fulltext_vehicletype")
* @Route("/{vehicleType}/{brand}", requirements={"vehicleType"="(araba|ticari-arac|motosiklet)", "brand"="[\w_-]+"}, name="offer_browse_vehicletype_brand")
* @Method({"GET"})
* @Template()
*/
public function searchAction(
    Request $request,
    $vehicleType = null,
    $brand = null,
) {
     ---
  }

for the first routing "brand" var may come as a GET parameter, but in twig path and url function passes this variable it still gets "brand" var as url parameter which I give null in action function definition. 对于第一个路由“ brand” var可能作为GET参数出现,但是在树枝路径和url函数传递此变量时,它仍将“ brand” var作为url参数,我在动作函数定义中给出了null。

I tried below line for setting route params but it didn't worked. 我尝试在下面的行中设置路线参数,但没有成功。 How could I prevent this overriding ? 我如何防止这种压倒一切? I have to use same variable name on routing and get, if not it will break dynamism all over the code. 我必须在路由上使用相同的变量名并获取,否则将破坏整个代码的动态性。

$request->attributes->set('_route_params', array_filter($request->get('_route_params')));

Do you have any idea ? 你有什么主意吗 ?

The variables in the route definition (and passed to controller action method) is a request attribute hence it any data in the URL won't get automatically assigned to the variable in your action method. 路由定义中的变量(并传递给控制器​​操作方法)是一个请求属性,因此,URL中的任何数据都不会自动分配给您的操作方法中的变量。

You can use $request->get("some name") which looks up for the variable in the order of 您可以使用$ request-> get(“ some name”)来按以下顺序查找变量

URL params > Request Attributes > Post params URL参数>请求属性>发布参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM