Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Building Websites with PHP Slim Basics & Twig Templates Including & Running Slim

I am working on my local pc. I am getting the following error message "Fatal error: Class 'Slim\Slim' not found in D:\"

I am working on my local pc. I am getting the following error message "Fatal error: Class 'Slim\Slim' not found in D:\". I googled but nothing worked for me. Any suggestions? Thanks

We need to see your index.php file and composer.json files please.

5 Answers

Change your code to this:

<?php
$app = new \Slim\Slim();

// from this:

$app = new Slim();

Thank you very much. You saved me a lot of time. :)

Yep, exactly--bad path.

You are welcome

Kevin Korte
Kevin Korte
28,111 Points

Are you using a localhost server like XAMPP, MAMP, etc?

I am using XAMPP. My Index.php code:

<?php
require __DIR__ . '/vendor/autoload.php';
//date_default_timezone_get('Asia/Dhaka');

/*$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Logger::WARNING));
$log->addWarning('Foo');*/

$app = new Slim();

$app->get("/", function () {
    echo "<h1>Hello Slim World</h1>";
});

$app->run();
?>

And composer.json code:

{
    "name": "nizam/test",
    "authors": [
        {
            "name": "Nizam Uddin",
            "email": "nishatsikder@protonmail.com"
        }
    ],
    "require": {
        "monolog/monolog": "^1.17",
        "slim/slim": "^2.6"
    }
}

edited comment for format code.

Kevin Korte
Kevin Korte
28,111 Points

Yep, I agree with Ted. You either need to do

<?php
$app = new \Slim\Slim();

or change your index.php to add a use Slim\Slim to the very top. Like So

<?php

use Slim\Slim;

require __DIR__ . '/vendor/autoload.php';
//date_default_timezone_get('Asia/Dhaka');

/*$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Logger::WARNING));
$log->addWarning('Foo');*/

$app = new Slim();

$app->get("/", function () {
    echo "<h1>Hello Slim World</h1>";
});

$app->run();
?>

The require line is interesting. I have:

<?php
require 'vendor/autoload.php';

I have seen a number of people that have this, though:

<?php
require __DIR__ . 'vendor/autoload.php';

My code runs in both development environments and at http://vetsrights.org. What is the difference?

Kevin Korte
Kevin Korte
28,111 Points

__DIR__ will be the absolute path to the directory where __DIR__ is called.

In your examples, its the difference between using relative and absolute paths. Either work, and the smaller the application, the less it matters.

For example, in a dev localhost

<?php
require __DIR__ . '/vendor/autoload.php';

might run as a filepath as

C:\xampp\htdocs\myapp/vendor/autoload.php

where that same line of code in a production ubuntu server might look like

/home/ubuntu/workspace/myapp/vendor/autoload.php

those are basically the same code base, var_dumped from my local XAMPP code and my Cloud9 workspace.

So __DIR__ is something (a special constant or function or something else) that is built into PHP that returns the absolute path to the project root? I did notice the concatenation, so assumed it was something like that.

Kevin Korte
Kevin Korte
28,111 Points

Yes, it's part of the core PHP library, known as a "magic" constant. Here is some more: http://php.net/manual/en/language.constants.predefined.php

Zeljko Porobija
Zeljko Porobija
11,490 Points

Creepy, but it works for me in the Workspace, but not on my Netbeans (Apache localhost). Any suggestion?