Вот ситуация: < /h3>
- Маршрут /дебаты < /code> (или /< /code>). Я выполняет все дебаты, но и то, что отлично работает. Чтобы получить доступ к конкретным дебатам, нажав на ссылку (например,/debatarena/debat/1 ), она возвращает ошибку 404 .
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)
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!
Подробнее здесь: [url]https://stackoverflow.com/questions/79512670/routing-issue-with-fastroute-in-php-app-404-page-not-found-when-accessing-a-spe
Мобильная версия