简体   繁体   中英

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.

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.

You can use $request->get("some name") which looks up for the variable in the order of

URL params > Request Attributes > Post params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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