简体   繁体   中英

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

I'm fairly new to the Slim framework and I'm getting an error in a very basic call $request->get .

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


Although var_dump($_GET) returns:

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

Contents of the index.php file:

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

The method get doesn't exist in Slim\\Http\\Request

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

You need to use getParam

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM