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

Logan Fox
PLUS
Logan Fox
Courses Plus Student 3,379 Points

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

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

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()
Rob Adams
Rob Adams
3,460 Points

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();
?>
Robert Kulagowski
Robert Kulagowski
4,954 Points

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?

Logan Fox
PLUS
Logan Fox
Courses Plus Student 3,379 Points

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