admin管理员组

文章数量:1389903

I'm working on a PHP application using FastRoute for routing, and I’m experiencing an issue when trying to access the detail page of a specific debate. The list of debates works fine, but when I try to access a specific debate, I get a 404 page not found error.

Here’s the situation:

  • The route /debats (or /) lists all the debates, and that works perfectly fine.
  • But when I try to access a specific debate by clicking on the link (e.g., /debatArena/debat/1), it returns a 404 error.

I suspect the issue might be related to how I handle the basePath and URL cleaning in my code, but I’m not sure what exactly is wrong.

Here’s the relevant code in my index.php:

require_once __DIR__ . '/../../vendor/autoload.php';

use FastRoute\RouteCollector;
use function FastRoute\simpleDispatcher;
use Controllers\DebatController;

// Define routes
$dispatcher = simpleDispatcher(function (RouteCollector $r) {
    $r->get('/', 'Controllers\DebatController@listDebats');  // List all debates
    $r->get('/debats', 'Controllers\DebatController@listDebats');  // Another route for listing debates
    $r->get('/debat/{id:\d+}', 'Controllers\DebatController@viewDebat');  // View a specific debate
});

// Get the requested URL
$httpMethod = $_SERVER['REQUEST_METHOD'];
$basePath = '/debatArena';  // The base path for the app

// Clean the URL
$uri = $_SERVER['REQUEST_URI'];
$uri = strtok($uri, '?');  // Remove query string
if (strpos($uri, $basePath) === 0) {
    $uri = substr($uri, strlen($basePath));
}

// Debugging output
var_dump("Original URL: $uri");

// Dispatch the request
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);

switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        http_response_code(404);
        echo "Page not found";
        break;

    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        http_response_code(405);
        echo "Method not allowed";
        break;

    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];

        // Instantiate the controller
        [$class, $method] = explode('@', $handler);
        $controllerInstance = new $class($pdo);

        // Call the controller method
        call_user_func_array([$controllerInstance, $method], $vars);
        break;
}

### Here's the code for the debats_list.php (the page showing all debates):

// Example: debats_list.php (just a simple version)
<?php foreach ($debats as $debat): ?>
    <div>
        <h2><?php echo $debat->getTitle(); ?></h2>
        <p><?php echo $debat->getDescription(); ?></p>
        <a href="/debatArena/debat/<?php echo $debat->getId(); ?>">View debate</a>
    </div>
<?php endforeach; ?>



What I'm trying to achieve:

    I have a list of debates, and I can navigate to each specific debate using links such as /debatArena/debat/1.
    However, when I try to access a specific debate, I get a 404 error.

What I've tried so far:

    I've ensured that the routes are defined correctly.
    I’ve tried cleaning the URL by removing the basePath and other extra parts like /src/web/.
    The list of debates works, but the problem happens only when trying to access a specific debate.

Has anyone encountered a similar issue with FastRoute in a PHP app, and how can I fix this issue to properly display the debate details?

Thanks in advance for your help!

本文标签: Routing issue with FastRoute in PHP app 404 page not found when accessing a specific debateStack Overflow