超薄框架 - 获取" 500内部服务器错误"一个简单的" $ request-> get"

时间:2016-05-09 10:36:26

标签: php rest slim

我对Slim框架相当新,我在一个非常基本的电话$request->get中收到错误。

  • provision / hosts - 确定
  • provision / hosts / 28E34748B48E - 确定
  • provision / hosts / search?hostname = ACACA - NOK


虽然var_dump($_GET)返回:

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();
?>

1 个答案:

答案 0 :(得分:1)

get

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

您需要使用getParam

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