问题
I'm making a REST service with php using the slim framework. everything works but there is something weird. I always get double or triple data. Here is my index.php:
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header( "HTTP/1.1 200 OK" );
exit();
}
function getConnection() {
try {
$db_username = "root";
$db_password="admin";
$conn = new PDO('mysql:host=localhost;dbname=dats24', $db_username);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
return $conn;
}
$app->get('/problem/find/:id/','getProblem'); // Using Get HTTP Method and process getUser function
$app->get('/problem/find-all/','getProblems'); // Using Get HTTP Method and process getUsers function
$app->post('/problem/add/', 'addProblem'); // Using Post HTTP Method and process addUser function
$app->delete('/problem/delete/:id','deleteProblem'); // Using Delete HTTP Method and process deleteUser function
$app->run();
function getProblems() {
$sql_query = "select * FROM problems ORDER BY Station";
try {
$dbCon = getConnection();
$stmt = $dbCon->query($sql_query);
$problems = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbCon = null;
echo '{"probfems": ' . json_encode($problems) . '}';
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getProblem($id) {
$sql = "SELECT * FROM problems WHERE idproblems=:id";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$problem = $stmt->fetchObject();
$dbCon = null;
echo json_encode($problem);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function addProblem() {
global $app;
$postdata = file_get_contents("php://input");
echo $postdata;
$req = json_decode($postdata);; // Getting parameter with names
$paramName = $req->station; // Getting parameter with names
$paramAdres = $req->address; // Getting parameter with names
$paramCity = $req->city;// Getting parameter with names
$parampostal = $req->postalcode;
$parampic = $req->pictureOfDamage;
$paramdescrip= $req->description;
$sql = "INSERT INTO problems (Station,Address,Postalcode,City,PictureOfDamage,Description) VALUES (:station,:address,:postalcode,:city,:pictureOfDamage,:description)";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam(':station', $paramName);
$stmt->bindParam(':address', $paramAdres);
$stmt->bindParam(':city', $paramCity);
$stmt->bindParam(':postalcode', $parampostal);
$stmt->bindParam(':pictureOfDamage', $parampic);
$stmt->bindParam(':description', $paramdescrip);
$stmt->execute();
$dbCon = null;
echo json_encode("toegevoegd ");
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function deleteProblem($id) {
$sql = "DELETE FROM problems WHERE idproblems=:id";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$dbCon = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
$app->run();
It doesn't matter wich method I pick like the /find-all gives this as output(the database is currently empty):
{"problems": []}{"problems": []}{"problems": []}
And if I preform a POST it adds it twice to the database. To even make it weirder I get double 404 error when I enter the wrong URL.

As last this is my .htaccess file
RewriteEngine On
RewriteBase /Dats24/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin"
the method's are not being called more then one time I checked it. I really don't know what the problem could be. Thanks in advance for the help :)
回答1:
That is because You have multiple lines with:
$app->run();
Each one executes the whole logic of Your application, and provides the whole output.
Run method should be executed always only once, after You configure routes, middleware etc.
As strange as it seems, I can not find this information (or anything about run method of Slim class) in Slim docs...
回答2:
Function always should return something.I have also faced such situation. But I solved it.In any function without echo you should try return.Sure it will work best.
来源:https://stackoverflow.com/questions/29743249/slim-framework-rest-service-getting-output-twice