简体   繁体   English

PHP致命错误:未找到类“ Slim”

[英]PHP Fatal error: Class 'Slim' not found

session_start();
date_default_timezone_set('GMT');
require 'Slim/Slim.php';
use Slim\Slim;
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

require_once 'item.php';

this is code excerpt from index.php and stuck on the said error when it called item.php . 这是index.php代码摘录,并在调用item.php时卡在上述错误上。 Here the contains of the file 这里的文件包含

$app->put('/getItem', authorize(), 'getItem');

function getItem() {
  $sql = "SELECT * FROM item";
  $app = Slim::getInstance();
  try {
   $db = getConnection();
   $stmt = $db->query($sql); 
   $item = $stmt->fetchAll(PDO::FETCH_OBJ);
   $db = null;
   $response = $app->response();
   $response->header('Content-Type', 'application/json');
   // Include support for JSONP requests
   if (!isset($_GET['callback'])) {
     echo json_encode($item);
   } else {
     echo $_GET['callback'] . '(' . json_encode($item) . ');';
   }
  } catch(PDOException $e) {
    $error = array("error"=> array("text"=>$e->getMessage()));
    echo json_encode($error);
  }
}

i hit the error on this $app = Slim::getInstance(); 我在$app = Slim::getInstance();遇到了错误$app = Slim::getInstance();

What is wrong with my approach? 我的方法有什么问题?

The Slim class' full name (including namespace) is \\Slim\\Slim so you'll need to use that, eg Slim类的全名(包括名称空间)为\\Slim\\Slim因此您需要使用它,例如

$app = \Slim\Slim::getInstance();

Alternatively, you can import the Slim symbol using the use statement at the top of your item.php script. 另外,您可以使用item.php脚本顶部的use语句导入Slim符号。

use Slim\Slim;

You can use this code for Slim Framework3 : 您可以将此代码用于Slim Framework3

<?php

require "vendor/autoload.php";
use \Slim\App;

$app = new App();

$app->get("/",function(){

    echo "Hello World";

});

$app->get("/test",function(){

    echo "Hello World";

});

$app->run();

?>

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

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