Slim Framework - 为简单的“$request->get”获取“500 Internal Server Error”

Slim Framework - Getting "500 Internal Server Error" for a simple "$request->get"

我是 Slim 框架的新手,我在一个非常基本的调用中遇到错误 $request->get


虽然var_dump($_GET)returns:

array(1) {
  ["hostname"]=>
  string(5) "ACACA"
}

index.php 文件的内容:

<?php
require 'vendor/autoload.php';

//With default settings
$app = new \Slim\App;

$app->get('/hosts', function ($request,$response,$args) {
        require_once 'db.php';
        $query= "SELECT * FROM hosts";
        $result = $mysqli->query($query);
        while($row=$result->fetch_assoc()) {
                $data[]=$row;
        }
        if(isset($data)) {
                header('Content-Type: application/json');
                echo json_encode($data);
        }
});

$app->get('/hosts/search', function ($request,$response,$args) {
        require_once 'db.php';
        //echo var_dump($_GET);
        $hostname=$request->get('hostname');
        echo $hostname;
});

$app->get('/hosts/{macaddr}', function ($request,$response,$args) {
        require_once 'db.php';
        $query= "SELECT * FROM hosts WHERE macaddr='".$args['macaddr']."'";
        $result = $mysqli->query($query);
        $data=$result->fetch_assoc();
        if(isset($data)) {
                header('Content-Type: application/json');
                echo json_encode($data);
        }
});

$app->run();
?>

方法 getSlim\Http\Request

中不存在
Fatal error: Call to undefined method Slim\Http\Request::get() in /slim3/index.php on line 

您需要使用getParam

$hostname = $request->getParam('hostname');