问题
I am getting this error and I haven't been able to fix it. All the solutions here use older Slim versions and are mostly about registering the autoloader, which is handled in this case.
What exactly causes this error? It says that it happens on the line in the function addJob()
with this code $request = Slim::getInstance()->request();
i.e. the Slim class is missing.
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->post('/add_job', 'addJob');
$app->run();
function addJob() {
$request = Slim::getInstance()->request(); // <------ ERROR
$job = json_decode($request->getBody());
$sql = "INSERT INTO jobs (title, company, description, location) VALUES (:title, :company, :description, :location)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("title", $job->title);
$stmt->bindParam("company", $job->company);
$stmt->bindParam("description", $job->description);
$stmt->bindParam("location", $job->location);
$stmt->execute();
$job->id = $db->lastInsertId();
$db = null;
echo json_encode($job);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
回答1:
What exactly causes this error?
- class Slim\Slim no longer exists
instead of getting request from statically shared instance, use one which is passed as first argument to your addJob function
function addJob(MessageInterface $request) {
$job = json_decode($request->getBody());
来源:https://stackoverflow.com/questions/34834950/php-fatal-error-class-slim-not-found-slim-framework-3