Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 GenerateURL to a complex route

This seems like the dumbest question ever, but I can't seem to figure it out. I have a page the needs to redirect to a complex route and I can't seem to generate the URL. Redirecting to a simple route is easy enough:

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

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

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

makes a URL of /testnumber?testid=1&question=4, which I do not want.

Edit: Yes, I already have the route created in a YML file. I simply cannot generate the 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 generate a URL to " testnumber/1", I need to generate "testnumber/1/question/4".

like image 904
rooter Avatar asked Nov 29 '22 14:11

rooter


1 Answers

For

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

to work as you want, your route must look like (example using annotations)

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

If you don't define "testid" & "question" parameters in your route, they'll be added to the query string (appended at the end of the URL as GET paramaters)

generated_route?test_id=X&question=X

Find here more relevent examples.

like image 196
Ahmed Siouani Avatar answered Dec 30 '22 01:12

Ahmed Siouani