Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,149 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,149 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,149 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,149 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,491 Points

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