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

Baur Alzhanov
Baur Alzhanov
7,028 Points

FATAL ERROR!

Fatal error: Uncaught Error: Class 'Slim\Slim' not found in /home/treehouse/workspace/index.php:9 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 9

My workspace don't working. What I need to do? Everywhere write this problem on line 9 both Slim and before

<?php require DIR . '/vendor/autoload.php'; date_default_timezone_set('Asia/Almaty');

//$log = new Logger('name'); //$log->pushHandler(new StreamHandler('app.txt', Monolog\Logger::WARNING)); //$log->addWarning('Oh Noes');

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name"; });

$app->run();

7 Answers

Lindsey Somerset
Lindsey Somerset
10,592 Points

Did you install the new version of Slim (3.0)? Or when you went through the install, did you just hit enter like Hampton said? Either way will install the most current version (3.0) which isn't the one Hampton uses.

The easiest way to follow along with his steps is to install version 2.6, the one he uses in the video. In your workspace console, just type:

composer require slim/slim "2.6"

This way you can use the same commands Hampton uses in the video. Please upvote/best answer if this helps!

I run into the same error. Actually, my error was this:

    ```html
     Fatal error: Class 'Slim\View' not found in /home/treehouse/workspace/vendor/slim/views/Twig.php on line 45


     ```

But what if we do want to use the newest version of slim? Can someone PLEASE help me out? I haven't been able to make any progress because of this error. I wish the teachers would update the video or at least put a note when using slim 3

  this is what my index.php looks like : 
  https://w.trhou.se/1t8sdedn7j

I couldn't get Hampton's code to work but there is different code at the slim website that looks like this

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->write("Hello " . $args['name']);

this worked for me.

Baur Alzhanov
Baur Alzhanov
7,028 Points

Doesn't work app.txt; Stopped to fix visit time.
I need to start SLIM. Now it doesn't want to work. (((

Baur Alzhanov
Baur Alzhanov
7,028 Points

Ok, Lindsey! Thank you!

ryan95
ryan95
8,964 Points

If you want to use the new version of Slim, then instead of using \Slim\Slim, you use \Slim\App. To make things easier then under the require statement you can add:

use \Slim\App AS Slim;

And just use Slim from then on, for example:

require("vendor/autoload.php");

use \Slim\App AS Slim;

$app = new Slim();

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->write("Hello " . $args['name']);
});

$app->run();

Did you figure it out? I still keep getting the same error, and I do not know how to fix it. My code looks the same, as in the video... and I am running V 3.

<?php
require 'vendor/autoload.php';
date_default_timezone_set('America/Chicago');


//$log = new Logger('name');
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
//$log->addWarning('Oh Noes.');

$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();
Baur Alzhanov
Baur Alzhanov
7,028 Points

Hi James. Twig and another components don't work in Slim Version 3. You need to reinstall version v2.6 in console. First you need to clean you workspace, remove in last video, install composer, then Slim. When you will be installing Slim in console need to write 2.6. Code string in Slim version 3 are large

Shehan Dissanayake
Shehan Dissanayake
19,119 Points

Hey Baur Alzhanov,

I found the solution, use the code below

$app = new \Slim\App();

$app->get('/', function($request, $response, $args) {
    echo 'Hello, world!';
});

$app->run();
?>