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

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

$ app = new \ Slim \ Slim();

require_once'item.php';
</ code> </ pre>

这是 index.php </ code>的代码摘录,并在调用 item.php </ code>时卡在上述错误上。 这里包含文件</ p>

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

function getItem(){\ n $ sql =“SELECT * FROM item”;
$ app = Slim :: getInstance();
尝试{
$ db = getConnection();
$ stmt = $ db-&gt; query($ sql );

$ item = $ stmt-&gt; fetchAll(PDO :: FETCH_OBJ);
$ db = null;
$ response = $ app-&gt; response();
$ response-&gt; header('Content -Type','application / json');
//包括对JSONP请求的支持
if(!isset($ _ GET ['callback'])){
echo json_encode($ item);
} else {
echo $ _GET ['callback']。 '('。json_encode($ item)。');';
}
} catch(PDOException $ e){
$ error = array(“error”=&gt; array(“text”=&gt; $ e-&gt; getMessage()));
echo json_encode($ error);
}
}
</ code> </ pre>

我点击了这个 $ app = Slim :: getInstance(); </ code> </ p>

我的方法出了什么问题?</ p>
</ div>

展开原文

原文

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?

dongxia4880
dongxia4880 你能做Slim::registerAutoloader();代替\Slim\Slim::registerAutoloader();?
6 年多之前 回复

2个回答



Slim </ code>类的全名(包括命名空间)是 \ Slim \ Slim </ code >所以你需要使用它,例如</ p>

  $ app = \ Slim \ Slim :: getInstance(); 
</ code> </ pre>

或者,您可以使用 item.php </ code>脚本顶部的 use </ code>语句导入 Slim </ code>符号。</ p>

 使用Slim \ Slim; 
</ code> </ pre>
</ div>

展开原文

原文

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;

duanji4449
duanji4449 谢谢@Phil。
6 年多之前 回复



您可以将此代码用于 Slim Framework3 :</ p>

 &lt;?php 

require“vendor / autoload.php”;
use \ Slim \ App;

$ app = new App();

$ app-&gt; get(“/”,function(){

echo“Hello World”;

});

$ app-&gt ; get(“/ test”,function(){

echo“Hello World”;

});

$ app-&gt; run();

?n?&gt; \ n </ code> </ pre>
</ div>

展开原文

原文

You can use this code for 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();

?>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问