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 Layouts with Twig

Sean Flanagan
Sean Flanagan
33,235 Points

Fatal error

Hi. I got this fatal error when previewing:

Fatal error: Class 'Slim\Views\Twig' not found in /home/treehouse/workspace/index.php on line 11

Here's my index.php:

<?php

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

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

$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));

$view = $app->view();
$view->parserOptions = array(
    'debug' => true,
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get("/", function() use($app) {
  $app->render("about.twig");
});

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

$app->run();

?>

Here's a snapshot:

https://w.trhou.se/fxtxoicn1h

Thanks in advance for any help. :)

5 Answers

Hi Sean,

It looks like you missed a few steps in earlier videos which had you include the 'twig/twig' and 'slim/views' packages into your projects.

I would review the following 2 videos around the 2:00 mark where you enter some console commands for composer.

adding twig/twig:
https://teamtreehouse.com/library/building-websites-with-php/slim-basics-twig-templates/installing-twig

adding slim/views:
https://teamtreehouse.com/library/building-websites-with-php/slim-basics-twig-templates/views-twig-inside-of-slim

I forked your workspace and then added in those packages. At that point I had a slim application error having to do with about.twig

I put quotes around main.twig and I was able to see the web page at that point.

about.twig
{% extends "main.twig" %}

I don't know if you're missing other things but the page is at least loading for me when I do those steps.

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Try to remove commas at the end of functions

$view->parserOptions = array(
    'debug' => true    // without comma at the end
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension()    // without comma at the end
);

Hope this will help

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Sergey. Thanks for your help. I've given you an up vote for your support, but unfortunately I'm still getting the same error message I had before. I'd be grateful for further help if that's okay, whoever it's from. Sean :-)

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Check also your .htaccess file.

Also you can check code in my project, which works properly - https://github.com/SergeyPodgornyy/Treehouse-PHP_Website

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Class 'Slim\Views\Twig' not found in the following directory /home/treehouse/workspace/index.php Try to write

require __DIR__ . '/vendor/autoload.php';

instead of

require 'vendor/autoload.php';

If this will not help, try on local machine. Perfect for this purpose is Linux.

Sean Flanagan
Sean Flanagan
33,235 Points

The matter is now sorted. Jason, Sergey, thank you for your help. And Jason Anders, I noticed you posted and then deleted. Thanks for your efforts too. :-)

Sean Flanagan
Sean Flanagan
33,235 Points

No preview available for my .htaccess file. I'm looking at your code now. Thanks for sharing it.