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 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?