1

I have a route defined like this (controller has a prefix /email):

/** * Opens send dialog * * @Route("/compose/{redirect_to}", name="email_compose", defaults={"redirect_to" = "/"}) * @Method({"GET", "POST"}) * @Template("EmailBundle:Email:compose.html.twig") */ 

I try to call this route via a javascript ajax POST, but even with a GET request I have routing errors:

http://example.com/app_dev.php/email/compose/%2Fapp_dev.php%2Fcurrent%2Fdetail%2F7

This throws a 404 error:

The requested URL /app_dev.php/email/compose//app_dev.php/current/detail/7 was not found on this server

If I try a simpler call like this one, it works properly:

http://example.com/app_dev.php/email/compose/aaa

How can I pass a parameter in querystring that is an URI?

Thank you

2 Answers 2

2

You can by allowing slashes in the corresponding route, e.g. :

compose: pattern: /compose/{redirectTo} # [defaults] requirements: redirectTo: ".+" 

The corresponding doc here.

But, because you are not trying to use an Ajax routing (in this case the custom route is mandatory), there is no reason to pass your target url as a route param.

(pass it as a query would be /compose?redirecTo=[redirectTo]

Also, as stated by @johnSmith (too fast!), you should use a POST request by require it like so in your route :

other: pattern: /compose # [defaults] requirements: methods: POST 

Then, just retrieve the param like he said, and create your redirection.

Sign up to request clarification or add additional context in comments.

4 Comments

The thing is, I prefer this param in querystring as route can be called via GET or POST, so in GET calls it will be easier to compose the route. I still get a 404 with this @Route("/compose/{redirect_to}", name="email_compose", defaults={"redirect_to" = ""}, requirements={"redirect_to"=".+"})
BTW, I'm doing an Ajax call
I think the double // in your route can be the cause of the problem (make the route restarted just after the hostname) . Try to remove the first / in your redirectTo and that should works with the requirements you added.
I'm not sure about what I said because the route is entirely interpreted as the error say. I'm not familiar with the @Route annotation, be sure that the requirements are correctly, maybe by looking in the doc.
2

you should use POST as Method and add the redirect route to the formdata

sth liek this

$.ajax({ url: '/path/to/file', type: 'POST', data: {redirectTo: '/current/detail/7'}, }) 

and in controller you parse this data like

$redirectTo=$this->getRequest()->get('redirectTo'); 

so you wont have any trouble

you would also not have any trouble if you urlEncode the redirectTo and append as GET parameter, e.g after ? you could retrieve it then with the same code as above, but having an url as fragment of a route doesnt make too much sense, but that may also be possible with allowing / to be a valid character in parameter

http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html

1 Comment

I applied both solutions, POST and use of ? in querystring instead of routing parameter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.