strtok the resques url before right trimming the /#52
strtok the resques url before right trimming the /#52ClementGre wants to merge 2 commits intophprouter:mainfrom
Conversation
The request_url should firstly be split to remove everything after the first ``?``: > $request_url = strtok($request_url, '?'); And only then, the $request_url should be right trimmed to remove the last ``/``: > $request_url = rtrim($request_url, '/'); Theses two lines were inverted.
|
Hi @ClementGre, very good observation and excellent way to explain it. The router is there to avoid query strings. So in your example "https://example.com/pricing/?type=0" the route should be "pricing/$type". Am I missing something? |
|
You are right, then it is not possible to manage url with GET arguments in PHP Router. Using friendly URLs is often the best choice, but in some cases you would need to use GET arguments, when working with GET forms or just sending links in mails that you want to be formatted with GET arguments. Adding the support for GET arguments might be a good idea. |
|
Line 50-51: Following code need to rearrange again. |
|
Any update on this? |
|
I agree that the order of operations should be reversed. Or better yet, Iin order to get the "path" part of the request URI (which your code is referring to as the "route") it might be more appropriate to consider using parse_url() to break down the request into its individual components, such as the scheme, host, port, path, query, and fragment. $route_parts = explode('/', $route);
array_shift($route_parts);
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
$request_path = parse_url($request_url, PHP_URL_PATH));
$request_path = rtrim($request_url, '/');
$request_path_parts = explode('/', $request_path);
array_shift($request_url_parts); |
In the original code :
The trim was done before the split. But this leads to issues when dealing with paths like this one :
https://example.com/pricing/?type=0Indeed, the first line will trim an unexisting
/at the end of the request url.And the second will remove the GET query parameters.
Resulting in this :
https://example.com/pricing/This left a remaining right
/that will create an element at the end of the exploded array.Then, the condition:
will be evaluated as true and the router will return while it should not.
The request_url should firstly be split to remove everything after the first
?:And only then, the $request_url should be right trimmed to remove the last
/:Then, I just inverted these two lines.