Symfony2 GenerateURL on a difficult route

This seems to be the dumbest question, but I can't figure it out. I have a page that needs to be redirected to a complex route, and I cannot create a URL. Redirecting to a simple route is quite simple:

return $this->redirect($this->generateUrl('testnumber')); 

However, I want to go to: testnumber / 1 / question / 4. How can I accomplish this incredibly simple task? The only thing I found in the documentation, and Google allows me to add parameters, and not just create complex routes. For instance:

 generateURL('testnumber', array('testid'=>1, 'question'=>4)) 

does url / testnumber? testid = 1 & question = 4, which I don't want.

Edit: Yes, I already have the route created in the YML file. I just can't create a URL to link to it.

  return $this->redirect($this->generateUrl(???????????),true)); 

This is my route:

 @Route("/testnumber/{testid}/question/{question}", name="testnumber") 

The symfony documentation only shows how to create a url for "testnumber / 1", I need to create "testnumber / 1 / question / 4".

+4
source share
1 answer

For

 generateURL('testnumber', array('testid'=>1, 'question'=>4)) 

to work as you want, your route should look like (e.g. using annotations)

 @Route("/testnumber/{testid}/question/{question}", name="testnumber") 

If you do not define the "testid" and "question" parameters in your route, they will be added to the query string (added at the end of the URL as GET parameters)

 generated_route?test_id=X&question=X 

Find more relevant examples here.

+21
source

Source: https://habr.com/ru/post/1436832/


All Articles