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 trialNizam Uddin
1,735 PointsI 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
5 Answers
Ted Sumner
Courses Plus Student 17,967 PointsChange your code to this:
<?php
$app = new \Slim\Slim();
// from this:
$app = new Slim();
Nizam Uddin
1,735 PointsThank you very much. You saved me a lot of time. :)
James Gill
Courses Plus Student 34,936 PointsYep, exactly--bad path.
Ted Sumner
Courses Plus Student 17,967 PointsYou are welcome
Kevin Korte
28,149 PointsAre you using a localhost server like XAMPP, MAMP, etc?
Nizam Uddin
1,735 PointsI 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"
}
}
Ted Sumner
Courses Plus Student 17,967 Pointsedited comment for format code.
Kevin Korte
28,149 PointsYep, 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();
?>
Ted Sumner
Courses Plus Student 17,967 PointsThe 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
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.
Ted Sumner
Courses Plus Student 17,967 PointsSo __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
28,149 PointsYes, 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
11,491 PointsCreepy, but it works for me in the Workspace, but not on my Netbeans (Apache localhost). Any suggestion?
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsWe need to see your index.php file and composer.json files please.