Dear Friend,
When i install slim 3 with below command
/opt/php-install/bin/php /root/composer.phar require slim/slim “^3.0”
all post variables in ( $value = json_decode($request->getBody())
give me null values.
URl/index.php/login/wines_add or URl/index.php/login/wines/6
Kindly help to find the way to ready post variables sending in request.
$app->group(’/login’, function () {
$this->post(’/wines_add’,‘addWine’,function ($request,$response,$args){});
$this->put(’/wines/{id}’, ‘updateWine’,function ($request,$response,$args){});
$this->delete(’/wines/{id}’, ‘deleteWine’,function ($request,$response,$args){});
} );
function addWine($request, $response, $args) {
$value = json_decode($request->getBody());
$sql = "INSERT INTO wine (name, grapes, country, region, year, description) VALUES (name, :grapes, :country, :region, :year, :description)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("name", $value->name);
$stmt->bindParam("grapes", $value->grapes);
$stmt->bindParam("country", $value->country);
$stmt->bindParam("region", $value->region);
$stmt->bindParam("year", $value->year);
$stmt->bindParam("description", $value->description);
$stmt->execute();
//$wine->id = $db->lastInsertId();
//$db = null;
echo json_encode($value);
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function updateWine($request, $response, $args) {
$id=$args[‘id’];
//echo “$id”;
// $body = $request->getBody();
// $wine = json_decode($body);
$value = json_decode($request->getBody());
$data = $request->getParams();
#echo “$data”;
#$data = $value->country;
#$paramValue = $app->$this->request->post(‘country’);
$sql = "UPDATE wine set name=:data WHERE id=$id";
// $sql = “select * from wine where name=:data”;
// $sql = “select * from wine where name=‘piper’”;
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(“data”, $value->country);
// $stmt->bindParam(“id”, $wine->id);
$stmt->execute();
// $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
echo '{“wine”: ’ . json_encode($value) . ‘}’;
//$db = null;
echo json_encode($value);
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}