以下是我的Router类的方法。该类将请求路由到用户定义的回调函数。我遇到的问题是标题返回404的$ status,即使在foreach循环中的条件中$ status设置为200。我已经回复了$ status并确认它已设置为200.虽然,如果我在声明中将$ status的值设置为200,它可以正常工作。
public function run()
{
$current_route = null;
$routes = null;
$status = 404; // http status code
if ($this->request_method == 'GET') {
$routes = $this->get_routes;
}
foreach ($routes as $route) {
if ($route['path'] == $this->url_path) {
$status = 200;
$current_route = $route;
break;
}
}
if ($status == 200) {
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
call_user_func($current_route['callback'], $this->app);
exit();
} else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit();
}
}