Ich arbeite an einer PHP -Anwendung mit Fastroute für das Routing und habe ein Problem, wenn ich versuche, auf die Detailseite einer bestimmten Debatte zuzugreifen. Die Liste der Debatten funktioniert einwandfrei, aber wenn ich versuche, auf eine bestimmte Debatte zuzugreifen, erhalte ich eine 404 -Seite, die nicht gefunden wurde. Um auf eine bestimmte Debatte zuzugreifen, indem Sie auf den Link klicken (z. B./debatarena/debat/1 ), wird ein 404 -Fehler zurückgegeben. /> Hier ist der relevante Code in meinem index.php < /code>: < /h3>
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)
[url=/debatArena/debat/">View debate[/url]
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 [url=viewtopic.php?t=15738]problem[/url] 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!
Ich arbeite an einer PHP -Anwendung mit [b] Fastroute [/b] für das Routing und habe ein Problem, wenn ich versuche, auf die Detailseite einer bestimmten Debatte zuzugreifen. Die Liste der Debatten funktioniert einwandfrei, aber wenn ich versuche, auf eine bestimmte Debatte zuzugreifen, erhalte ich eine 404 -Seite, die nicht gefunden wurde. Um auf eine bestimmte Debatte zuzugreifen, indem Sie auf den Link klicken (z. B./debatarena/debat/1 ), wird ein 404 -Fehler zurückgegeben. /> Hier ist der relevante Code in meinem index.php < /code>: < /h3> [code]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
// 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)
[url=/debatArena/debat/">View debate[/url]
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 [url=viewtopic.php?t=15738]problem[/url] 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?
Ich habe Code, um eine dauerhafte Authentifizierung mit .htaccess durchzuführen, was funktioniert, aber nur beim Zugriff auf Verzeichnisse oder Unterverzeichnisse, z. B. / funktioniert, aber...
Ich lerne, wie man LiveWire und Laravel verwendet. Ich habe versucht, einige Daten über einen Eingang zu binden
Ich habe diesen Code geschrieben:
Home.blade.php:
Ich erstelle eine Chat-Anwendung mit PHP im Backend und React.js im Frontend. Allerdings stoße ich auf ein Problem mit der Abruf-API.
Mein Abruf, der einen Endpunkt aufruft
fetch('
method:'POST',...
Mein Django-Projekt ist eine Einzelanwendungs-Website, deren Homepage Artikel/Beiträge aus meiner Datenbank zeigt und Seiten für Tag-Themen und Jahres-/Monatsarchive enthält. Alle anderen Seiten sind...
Ich arbeite an einem Next.js 14-Projekt, das im Entwicklungsmodus perfekt funktioniert ( next dev ). Nach der Erstellung des Projekts ( next build ) und starten ( next start oder als...