php - 为什么 POST 被解释为 GET?
问题描述
我正在尝试使用提交表单设置基本路由。
HTML:
<form method="POST" action="/names">
<input name="name"></input>
<button type="submit">Submit</button>
</form>
PHP:
class Router {
public $routes = [
'GET' => [],
'POST' => []
];
public function get($uri, $controller){
$this->routes['GET'][$uri] = $controller;
}
public function post($uri, $controller){
$this->routes['POST'][$uri] = $controller;
}
public function direct($uri, $requestType){
if(array_key_exists($uri, $this->routes[$requestType])){
return $this->routes[$requestType][$uri];
}
throw new Exception('No route defined for this uri');
}
}
$router = new Router;
$router->get('', 'index.view.php');
$router->post('names', 'add-name.php');
class Request {
public static function uri(){
return trim(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),'/'
);
}
public static function method(){
return $_SERVER['REQUEST_METHOD'];
}
}
require $router->direct(Request::uri(), Request::method());
但每次我提交时,它只会说“没有为此 uri 定义路由”而且我认为问题在于 POST 请求被解释为 GET。当我尝试
var_dump(Request::uri());
var_dump(Request::method());
它说 string(5) "names" string(3) "GET"
我真的很困惑为什么会这样。任何想法为什么 POST 切换到 GET?
解决方案
推荐阅读
- flutter - http.get 返回 404“找不到路由”
- xcode - 合并冲突 看不到“?” 在 xcode 中
- swift - 添加 Swift 包时 Xcode 崩溃
- flutter - 如何使用 dart 在 Firestore 查询中更新值
- database - 在 DB2 存储过程中存储 1000 个字符的大字符串
- python-3.x - 无法在 Gunicorn 上部署 dash 应用程序
- ubuntu - 错误:找到了 SUID 沙箱帮助程序二进制文件,但未正确配置
- python - Tensorflow 2 对象检测 API - 如何使用减小的深度乘数训练 MobileNetV2?
- python - 如果不存在则添加文件,否则只需更新日期
- python - Lambert Solver 的传播解导致错误的轨道