bramus / router

A lightweight and simple object oriented PHP Router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Router only picks up the index route, ignores any other route

erolsmsr58 opened this issue · comments

I use a Controller Factory to build my controllers and use the router by just passing the URI. The router should simply trigger and do the same thing for any URI in my application. However, the router only triggers on the index route (/) and ignores any other.

My code looks as follows:

public function handle(): Response
{
    $response = null;

    $request = new Request($_REQUEST);
    $uri = $request->getRequestUri();

    $container = $this->configureContainer();
    $renderer = new RendererFactory();
    $router = $this->configureRouter();

    $controllerFactory = new ControllerFactory();
    $controller = $controllerFactory->create($request, $container, $renderer, $uri);

    $router->get($uri, function () use ($uri, $controller, &$response) {
        $response = $controller->get();
    });

    $router->run();

    return $response;
}

When I navigate to the index route on my browser, it all goes fine. However, when I for example navigate to /about-us, the $router->get() doesn't trigger at all. Even if I hardcode it like $router->get('/about-us', function () use ($uri, $controller, &$response) it'll just ignore it. The anonymous function inside the get doesn't fire AT ALL.

I've confirmed, using var_dump, that I have a valid container with everything in there to make this work, the router simply won't trigger on the URI that I pass to it, even if the URI has been confirmed to look fine. When I var_dump the $uri variable, it says /about-us, and even hardcoding that value as a string doesn't make it trigger.

What's the reason for this?

I added the following code to the Router.php file of the Bramus router to debug

I added a var_dump() in the handle function inside the package itself, and it always says that the result of $this->getCurrentUri() is /, and not the URI in the browser.

My .htaccess is in the root directory and I redirect all requests to /public/index.php. Maybe that's the culprit? But I don't know how to fix it. My .htaccess:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/index.php?path=$1 [NC,L,QSA]

#82 (comment)

This answered my question, will close this issue now.