简体   繁体   English

PHP致命错误:未找到类'Slim'-Slim Framework 3

[英]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. 此处的所有解决方案都使用较旧的Slim版本,并且大多与注册自动加载器有关,在这种情况下,将进行处理。

What exactly causes this error? 究竟是什么导致此错误? It says that it happens on the line in the function addJob() with this code $request = Slim::getInstance()->request(); 它说它发生在函数addJob()的行上,代码$request = Slim::getInstance()->request(); ie the Slim class is missing. 即Slim类丢失。

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 Slim \\ Slim类不再存在

instead of getting request from statically shared instance, use one which is passed as first argument to your addJob function 与其从静态共享实例获取请求,不如使用第一个参数传递给您的addJob函数的请求

function addJob(MessageInterface $request) {
    $job = json_decode($request->getBody());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM