phpslimslim-3

Slim 3 parameter with slash


I have an API built with Slim 3, a route is defined as

$app->get('/usage/{protocol}', function(Request $request, Response $response, array $args) {
  $protocol = !empty($args['protocol']) ? sanitize($args['protocol']) : null;
  // do something...
});

My problem is that the protocol parameter can have a slash character in it multiple times / i.e.: 2022-123/A/B/C.

When I make a request for http://example.com/api/usage/2022-123/A/B/C I get the following error:

GET http://example.com/api/usage/2022-123/A/B/C resulted in a 404 Not Found response.

How do I fix this so that it treats the whole text as one parameter?


Solution

  • try this

    $app->get('/usage[/{protocol:.*}]', function(Request $request, Response $response, array $args) {
      $protocol = $request->getAttribute('protocol')
      // do something...
    });