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 & Rendering

Handy Metellus
Handy Metellus
7,796 Points

Help rendering with slim

I'm seeing this error when trying to render the index and contact page with the latest version of slim installed with composer: "Slim Application Error A website error has occurred. Sorry for the temporary inconvenience".

Here is my code, the 2 files our in my template folder:

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

require 'vendor/autoload.php';

date_default_timezone_set('America/New_York');

/**use Monolog\Logger; use Monolog\Handler\StreamHandler;

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

$app = new \Slim\App;

$app->get('/', function() use($app){ $app->render('index.html'); });

$app->get('/contact', function() use($app){ $app->render('contact.html'); });

$app->run();

5 Answers

Darrell Conklin
Darrell Conklin
22,099 Points

If you followed along with the teacher when he installed slim using composer then you are no doubt using a different version of Slim then the teacher since it has been updated.

In order to follow along with this video I recommend switching to the 2.6.3 version. Simply go your composer.json file and and change the version of slim to:

"require": {
        "monolog/monolog": "^1.22",
        "slim/slim": "2.6.3"
    }

Then open your console and type

composer update

That should update slim to a similar version of what the teacher is using.

You should now be able to follow along with the teacher.

Lin Lu
Lin Lu
29,171 Points

Hi Handy,

I copied your code and pasted in the workspace. The error I got was:

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

I simply changed "\Slim\App" to "\Slim\Slim", it worked fine! Also, it is unnecessary to add the two import statements at the beginning of the file, since they are not used. You could remove:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
Handy Metellus
Handy Metellus
7,796 Points

Hi,

Thanks for the reply but its still not working. I'm using the latest version of slim which is 3.4. Also, I'm using computer as localhost with MAMP and sublime text.

Thanks

Hi Handy,

I have pretty much the same code as you and it was working fine until I placed the index and contact html files into the template folder.

Here is my code which works well when those two files stay in the root folder.

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set("Australia/Hobart");

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use \Slim\App AS Slim;

// Create and configure Monolog Logger app
$log = new Logger('name');
$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
$log->addWarning('Index.php Started');

// Create and configure Slim app
$app = new Slim;
//$app = new \Slim\Slim();

// Define app routes
$app->get('/', function() use($app){
    $app->render('index.html')
});

$app->get('/contact', function() use($app){
    $app->render('contact.html')
});


// Run app 
$app->run();

Are you using Slim version 3? make sure you use the version the instructor is using. Start from the beginning by deleting the folders in your workspace and reinstalling composer, monolog, slim etc... I also run into problems when using Slim version 3., I restarted the project using the previous version of Slim and have been making a lot of progress .