PHP Fatal error: Class 'Slim' not found

require 'Slim/Slim.php';
use Slim\Slim;

$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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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;

Solution 2

You can use this code for Slim Framework3:


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

$app = new App();


    echo "Hello World";



    echo "Hello World";




Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply