douxiong2999 2014-03-13 04:48
浏览 75
已采纳

PHP致命错误:找不到类'Slim'

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

What is wrong with my approach?

  • 写回答

2条回答 默认 最新

  • dslk6326846 2014-03-13 04:52
    关注

    The Slim class' full name (including namespace) is \Slim\Slim so you'll need to use that, eg

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

    Alternatively, you can import the Slim symbol using the use statement at the top of your item.php script.

    use Slim\Slim;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)