Skip to content

It's throwing an error when add // for the domain URL like https://example.com// #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
usman342 opened this issue Apr 17, 2024 · 2 comments

Comments

@usman342
Copy link

Fatal error: Uncaught Error: Typed property Pecee\SimpleRouter\Router::$request must not be accessed before initialization in /vendor/pecee/simple-router/src/Pecee/SimpleRouter/Router.php:221 Stack trace: #0 /vendor/pecee/simple-router/src/Pecee/SimpleRouter/Router.php(295): Pecee\SimpleRouter\Router->processRoutes() #1 /vendor/pecee/simple-router/src/Pecee/SimpleRouter/Router.php(337): Pecee\SimpleRouter\Router->loadRoutes() #2 /vendor/pecee/simple-router/src/Pecee/SimpleRouter/SimpleRouter.php(69): #6 {main} thrown in /vendor/pecee/simple-router/src/Pecee/SimpleRouter/Router.php on line 221

@usman342 usman342 changed the title It throwing an error when add // for the domain URL like https://example.com// It's throwing an error when add // for the domain URL like https://example.com// Apr 23, 2024
@foobarhl
Copy link

foobarhl commented Nov 19, 2024

It appears that PHP's inbuilt 'parse_url' method called by the Url() class does not like the extra slashes. parse_url will return the component array if a single '/' is passed but fails with '//', '///', etc. (tested on php 8.3.6)

A quick and dirty work around is to strip consecutive slashes prior to SimpleRouter being loaded (i put this before my include vendor.php etc.)

while(strpos($_SERVER["REQUEST_URI"],'//') !== false )
    $_SERVER["REQUEST_URI"] = str_replace('//','/',$_SERVER['REQUEST_URI']);

it's not great, but kind of works around the problem.

@parisl69
Copy link

Indeed this seems to be an issue! Consecutive slashes throw an error.
However, I also wanted the url in the browser's bar to look correct as well, so here's another approach which I'm trying.

In my index.php as well like @foobarhl did.

$originalUri   = $_SERVER["REQUEST_URI"];
$normalizedUri = preg_replace('/\/+/', '/', $originalUri);

if ($normalizedUri !== $originalUri) {
    header("Location: {$normalizedUri}", true, 301);
    exit;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants