Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php nikic/FastRoute post请求参数访问_Php_Fastroute - Fatal编程技术网

Php nikic/FastRoute post请求参数访问

Php nikic/FastRoute post请求参数访问,php,fastroute,Php,Fastroute,我正在尝试使用FastRoute实现简单的POST请求。我按照给定的示例成功地实现了GET类型请求。在实现POST请求时,我无法访问随请求发送的参数 $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('POST', '/login', 'Test/put'); $r->addRoute('GET', '/users/{id:\d+}', '

我正在尝试使用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";
 }
}

我试图检查$\u POST,但是,那是空的。

在搜索了大量关于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";
  }
}