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

olamilekan oshodi
olamilekan oshodi
959 Points

PHP Slim composer - Not Found

Hello, Am just in the middle of Build A Basic PHP Website and Hempton was using Slim Framework, everything is going fine until i get to installing Slim. Hempton used v2 in the tutorial and have manage to include the "^3.0" in my composer.json file.

here:

{
    "name": "root/workspace",
    "require": {
        "slim/slim": "^3.3"
    },
    "authors": [
        {
            "name": "Olamilekan Oshodi",
            "email": "*****ter@gmail.com"
        }
    ]
}

Here is the my index file code.

<?php

require 'vendor/autoload.php';
//date_default_timezone_set('Europe/London');


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

// installing Slim Framework
$app = new Slim\App();

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

$app->run();

//here is the error am getting.
Fatal error: Uncaught Error: Class 'Slim\App' not found in /home/treehouse/workspace/index.php:11 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 11

6 Answers

Bryah Jackson
Bryah Jackson
3,562 Points

olamilekan and shane,

I am using the Workspace to work with Slim for now, as I am not convinced it is the best way for me to learn PHP fully or the best framework to use in my own projects. I also have Slim v3.3 and was not able to load my Hello, $name.

I went to the Slim website to look at their basic examples for the "Hello World!" kind of application. I noticed an issue with these two lines in their example:

<?php

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

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

The issue is this: our file system does not have this exact setup in Workspaces. However, while stumbling about looking for these missing files, I found an example index.php file to work with:

vendor/slim/slim/example/index.php

In there was some documentation that helped me create a "Hello World!" app that worked. Here is my final code for it:

<?php

require 'vendor/autoload.php';

$app = new \Slim\App();

$app->get('/hello[/{name}]', function ($request,$response,$args) {

  $response->write("Hello, " . $args['name'] . ".");

  return $response;

});

$app->run();
Carole Nissen
Carole Nissen
5,655 Points

Excellent post, thank you Bryah - this is a great update that worked for me so I can carry on with the course (for now anyway!) :-)

Hi Olamilekan, i am having the same issue, the problem is the 3.0 update with the course in 2.6 from what i understand you can uninstall the 3.0 and use 2.6 in the treehouse workspace.

the issue I am having is I am using my own server and to learn on and i am trying to learn slim now to make it work i can get the Hello world to work but not the example i have been learning in the course.

you can find all the information here http://www.slimframework.com/docs/tutorial/first-app.html

they suggest using something like http://localhost:8080/hello/joebloggs to load the Hello World app.

olamilekan oshodi
olamilekan oshodi
959 Points

Thank you Shane, i will give it ago and see what the outcome is. The need to update this tutorial as we are subscribing to it and waiting one week to find out sort out this kind of problem is frustrating. Well in my opinion that's what i thought.

Thanks.

Yea I know I have spent three days trying to work out Slim 3 it is a lot more complex than 2.6 I have joined their site and forums, in the hope of learning it properly it is so new that there are no tutorials on it to learn it as a beginner. some people have posted solutions in the treehouse questions under the videos but i am running it on my computer through Mamp and i cannot get the course task to work, although i can do the basic hello $app.

olamilekan oshodi
olamilekan oshodi
959 Points

how also read alot of tutorial and post on the v3, but no matter what i do i can't get to work. I think i can't move on to the cause until i fix it, otherwise i can't follow properly.

Have now pause my enrollement to take a quick external tutorial on what have learnt so far with PHP and OOP. And maybe in the mean time i can figure out wether to skip the tutorial and move down to the track which is the database application.

I know what you mean i don't want to move on further either but i have been talking on the Slim forums they say the key to working with slim is learning more php, one you can understand it properly slim becomes very simple to use, and very easy to use. so maybe i will use 2.6 and pass the course and keep learning for now.