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

喜你入骨 提交于 2020-01-06 19:24:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!