1
votes

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

2 Answers

2
votes

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.

2
votes

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