纤薄框架 - 获得简单"$request->get" "500 Internal Server Error"



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

  • 配置/主机-正常
  • 配置/主机/28E34748B48E-正常
  • 供应/主机/搜索?hostname=ACACA-NOK


尽管var_dump($_GET)返回:

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

index.php文件的内容:

<?php
require 'vendor/autoload.php';
//With default settings
$app = new SlimApp;
$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();
?>

SlimHttpRequest 中不存在方法get

Fatal error: Call to undefined method SlimHttpRequest::get() in /slim3/index.php on line 

您需要使用getParam

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

最新更新