Home > Net >  Laravel - Proper way to support a URL piece that might have a slash in it?
Laravel - Proper way to support a URL piece that might have a slash in it?

Time:09-22

I've defined a route in web.php that looks like this:

Route::delete('/app/charges-for-order/{orderId}', 'AppController@deleteOrderCharges');

It seems to work well apart from when orderId has a forward slash in it. Eg 11/11.

Naturally my first port of call was to generate the url in the frontend using encodeURIComponent:

/app/charges-for-order/${encodeURIComponent(orderId)} (in javascript)

Which produces the url /app/charges-for-order/11/11, replacing the forward slash with / -- however, this is giving me a 404, as if Laravel is still seeing / as a forward slash for routing purposes.

How can I allow orderId to have (encoded) forward slashes and still be part of the URL like that? Is there some alternate encoding scheme that Laravel will respect in this context?

CodePudding user response:

I assume you are using Apache as HTTP server? At least Apache is by default decoding / behind your back: enter image description here

  • Related