MehdiB
MehdiB

Reputation: 906

Symfony % causing no route found

I have a controller with a generateQrCodeAction in it:

/**
 * @Route("/qrCode/generate/{eakte}")
 * @param $eakte
 * @return Response
 */
public function generateQrCodeAction($eakte) {
    $qrService = $this->get("app.qrcode");
    $qrService->generate_qr_code($eakte);
    return new Response("done");
}

the eakte parameter is a url encoded one, from a string containing "/" in it. url encoding "/" results in "%2F" in the eakte parameter. However when I test the route /qrCode/generate/800%2F08SL300001 as an example, I get a route not found error. it seems that % is not allowed in routes! is there a workaround for this?

Upvotes: 1

Views: 574

Answers (2)

beeftony
beeftony

Reputation: 307

For people still having this problem. I had to encode/decode the value twice for Symfony to properly accept it in the url:
When generating the url:

$this->generateUrl('route_name', [
    'value' => urlencode(urlencode($value)),
]);

In the controller:

#[Route('/route/{value}', name: 'route_name')]
public function indexByProperty(string $value): Response
{
    $decodedValue = urldecode(urldecode($value));
    // ...
}

Upvotes: 0

Gregoire Ducharme
Gregoire Ducharme

Reputation: 1108

This would allow you to have a "/" in your url parameter

/**
 * @Route("/qrCode/generate/{eakte}", requirements={"eakte"=".+"})
 */

Source: https://symfony.com/doc/current/routing/slash_in_parameter.html

Upvotes: 2

Related Questions