Question

I am using Silex 1.1 and PHP 5.3, how do I make the ending "/" optional in a route?

Currently I have two routes doing the same thing

$app->get('/item/{id}/', function( $id ) use ($app) {
  /** Codes Here **/
});
$app->get('/item/{id}', function( $id ) use ($app) {
  /** Codes Here **/
});

Is it possible to specify the route only once where the ending "/" is optional? and how?

Thanks

Was it helpful?

Solution

If you define the route with a trailing slash, any request without trailing slash will return a 301 redirect to the correct one. So define /item/{id}/ and any request to /item/{id} should return a 301 redirect to /item/{id}/

OTHER TIPS

I solved it replacing the PATH_INFO parameter in my own Request instance.

$request = Request::createFromGlobals();

$request->server->set('REQUEST_URI', rtrim($request->server->get('REQUEST_URI'), "/"));

$app->run($request);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top