flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.61k stars 407 forks source link

Slashes In Routes #492

Closed joey-trimyer closed 6 months ago

joey-trimyer commented 1 year ago

I'm writing a URL shortener and am running into an issue with slashes in routes. For my API I have an endpoint that looks something like: https://mydomain.com/shrinkMe/domain_to_shorten.html/short_link

I've got it working, except if I try to use a domain with slashes. Then, I get a 404 error.

I tried escaping the slashes with %2F and that didn't work either.

I updated Apache with AllowEncodedSlashes ON and am still getting a 404.

Any advice would be appreciated.

mjisoton commented 12 months ago

I know it's not the answer you are looking for, but if the endpoint has the desired behaviour of "processing a certain entity (an URL) and returning the result", it should probably be a POST request, and not GET. On a POST request, there's no problem: you send the URL on the payload, no need to worry about the content of the string.

If it really need to be a GET request, you can use the wildcard (*). Like this:

Flight::route('GET /shrinkMe/*/short_link', function(){
    $url = str_ireplace(array('/shrinkMe/', '/short_link'), '', trim(Flight::request()->url));

    var_dump($url);
});

I'm not sure if there's another way.

joey-trimyer commented 12 months ago

POST is fine and definitely the correct answer. I was trying to do something that was some combination of lazy and fancy, but POST is the way to go.

Thanks for the reply and for the very awesome project.