Sir Rubberduck
Sir Rubberduck

Reputation: 2292

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

Upvotes: 0

Views: 735

Answers (1)

V&#237;t Kutn&#253;
V&#237;t Kutn&#253;

Reputation: 26

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

Upvotes: 1

Related Questions