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

Slim framework not working

I have looked in the slim docs for the hello world code that he uses but I can't find it. I then typed out exactly what the instructor did but this is what I get

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

<?php

require __DIR__ . '/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();

5 Answers

What version are you using?

The videos are using version 2 and the latest version is 3.

If you are using version 3 (which I guess you are) you will have to use the docs a lot.

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});
$app->run()

May 2018 - Slim v3.10

Here's the code I was able to get to work for this video. It's a combination of Hampton's 'require' and 'date/time' fix and the code on the front page of the Slim site. Good luck! (This video really does need an update.)

<?php
    require __DIR__ . '/vendor/autoload.php';
    date_default_timezone_set('America/New_York');
    //$log = new Logger('name');
    //$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
    //$log->addWarning('Oh Noes');

    use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;

    $app = new \Slim\App;

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

    $app->run();
?>

If you remove the leading "/" before "vendor", does it work? If there's something wrong with the path, then it might be looking for vendor in the root directory, rather than as a subdirectory in your current environment?

You have to start with a '/' before continue your path when using DIR otherwise you will get some nasty errors :-P

so when I do that it says this:

Warning: require(/home/treehouse/workspacevendor/autoload.php): failed to open stream: No such file or directory in /home/treehouse/workspace/index.php on line 3

Fatal error: require(): Failed opening required '/home/treehouse/workspacevendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/treehouse/workspace/index.php on line 3

Thank you Thats it!

Glad I could help :-)

Here is a link to Slim3 https://www.slimframework.com/