简体   繁体   中英

PHP Fatal error: Class 'Slim' not found - Slim Framework 3

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(); ie 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() .'}}'; 
    }
}

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

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