亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

nikic/FastRoute post請求參數訪問

nikic/FastRoute post請求參數訪問

PHP
精慕HU 2023-09-22 14:55:52
我正在嘗試使用 FastRoute 實現簡單的 POST 請求。我按照給定的示例成功實現了 GET 類型請求。在實現 POST 請求時,我無法訪問隨請求發送的參數。$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {  $r->addRoute('POST', '/login', 'Test/put');  $r->addRoute('GET', '/users/{id:\d+}', 'Test/put');});$httpMethod = $_SERVER['REQUEST_METHOD'];$uri = $_SERVER['REQUEST_URI'];if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos);}$uri = rawurldecode($uri);$routeInfo = $dispatcher->dispatch($httpMethod, $uri);switch ($routeInfo[0]) { case FastRoute\Dispatcher::FOUND:  $handler = $routeInfo[1];  $vars = $routeInfo[2];  list($class, $method) = explode("/", $handler, 2);  call_user_func_array(array(new $class, $method), $vars);  break;}class Test { public function put() {  return "Check"; }}我試圖檢查$_POST,但是,它是空的。
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

在搜索了大量有關 nikic/FastRoute post 請求的信息后,對代碼進行了以下更改。

$_POST = json_decode(file_get_contents('php://input' ),true);


$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {

? $r->addRoute('POST', '/login', 'Test/put');

? $r->addRoute('GET', '/users/{id:\d+}', 'Test/put');

});


$httpMethod = $_SERVER['REQUEST_METHOD'];

$uri = $_SERVER['REQUEST_URI'];


if (false !== $pos = strpos($uri, '?')) {

?$uri = substr($uri, 0, $pos);

}


$uri = rawurldecode($uri);

$httpMethod = $_SERVER['REQUEST_METHOD'];

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);

switch ($routeInfo[0]) {

?case FastRoute\Dispatcher::FOUND:

? ?$handler = $routeInfo[1];

? ?$vars = ($httpMethod == 'POST')? $_POST : $routeInfo[2];;

? ?list($class, $method) = explode("/", $handler, 2);

? ?call_user_func_array(array(new $class, $method), $vars);

? ?break;

}


class Test {

? public function put() {

? ? return "Check";

? }

}



查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號