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: Class 'Slim\Views\Twig' not found in /home/treehouse/workspace/index.php on line 11

Hi.

I've got the above error message despite having added Slim\Views. Nothing I do works. It seems that every time I close my work space and come back to it later, this happens.

index.php:

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

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

// Instantiate new Slim application
$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.twig");;
});

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

about.twig:

{% extends "main.twig" %}

{% block content %}
  <strong>About</strong>
  <h2>Ralph Waldo Emerson</h2>

  <p>Ralph Waldo Emerson <em>(May 25, 1803 – April 27, 1882)</em> was an American essayist, lecturer, and poet, who led the Transcendentalist movement of the mid-19th century. He was seen as a champion of individualism and a prescient critic of the countervailing pressures of society, and he disseminated his thoughts through dozens of published essays and more than 1,500 public lectures across the United States.</p>

  <p>Emerson gradually moved away from the religious and social beliefs of his contemporaries, formulating and expressing the philosophy of Transcendentalism in his 1836 essay, Nature. Following this ground-breaking work, he gave a speech entitled "The American Scholar" in 1837, which Oliver Wendell Holmes, Sr. considered to be America's "Intellectual Declaration of Independence".</p>

  <p>Emerson wrote most of his important essays as lectures first, then revised them for print. His first two collections of essays – Essays: First Series and Essays: Second Series, published respectively in 1841 and 1844 – represent the core of his thinking, and include such well-known essays as Self-Reliance, The Over-Soul, Circles, The Poet and Experience. Together with Nature, these essays made the decade from the mid-1830s to the mid-1840s Emerson's most fertile period.</p>

  <p>Emerson wrote on a number of subjects, never espousing fixed philosophical tenets, but developing certain ideas such as individuality, freedom, the ability for humankind to realize almost anything, and the relationship between the soul and the surrounding world. Emerson's "nature" was more philosophical than naturalistic: "Philosophically considered, the universe is composed of Nature and the Soul." Emerson is one of several figures who "took a more pantheist or pandeist approach by rejecting views of God as separate from the world.""</p>

  <p>He remains among the linchpins of the American romantic movement, and his work has greatly influenced the thinkers, writers and poets that have followed him. When asked to sum up his work, he said his central doctrine was "the infinitude of the private man." Emerson is also well known as a mentor and friend of fellow Transcendentalist Henry David Thoreau.</p>

    <p><em>Learn more about Ralph Waldo Emerson from <a href="http://en.wikipedia.org/wiki/Ralph_Waldo_Emerson">Wikipedia</a></em></p>
{% endblock content %}

main.twig:

<!doctype html>

<html lang="en">
<head>
  {% block head %}
    <meta charset="utf-8">
    <title>{% block title %}Ralph Waldo Emerson{% endblock title %}</title>
    <meta name="description" content="Ralph Waldo Emerson">
    <meta name="author" content="Treehouse">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/master.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="js/global.js"></script>
  {% endblock head %}
</head>

<body>
<!--
  <div id="feedback" class="success">
    <h3>Success!</h3>
    <p>You're reading all about Emerson.</p>
  </div>
-->


  <header>
    <h1>Ralph Waldo Emerson</h1>
    <nav>
      <a href="index.html" class="selected">About</a>
      <a href="contact.html">Contact</a>
    </nav>
  </header>

  <div class="emerson">
    {% block hero %}<img src="images/emerson.jpg" alt="Picture of Ralph Waldo Emerson">{% endblock hero %}
  </div>

  <main>
    {% block content %}
    {% endblock content %}
  </main>

  <footer>
    {% block footer %}
      <p>A project from <strong><a href="http://teamtreehouse.com">Treehouse</a></strong></p>
      <nav>
        <a href="index.html" class="selected">About</a>
        <a href="contact.html">Contact</a>
      </nav>
    {% endblock footer %}
  </footer>

</body>
</html>

contact.twig:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ralph Waldo Emerson</title>
  <meta name="description" content="Ralph Waldo Emerson">
  <meta name="author" content="Treehouse">
  <link rel="stylesheet" href="css/master.css">
  <link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="js/global.js"></script>
</head>

<body>

<!--  <div id="feedback" class="fail">
    <a class="dismiss">Dismiss</a>
    <h3>Oops!</h3>
    <p>Looks like you forgot to enter your email address.</p>
  </div>
-->

  <header>
    <h1>Ralph Waldo Emerson</h1>
    <nav>
      <a href="index.html">About</a>
      <a  href="contact.html" class="selected">Contact</a>
    </nav>
  </header>

  <div class="emerson">
    <img src="images/emerson.jpg" alt="Picture of Ralph Waldo Emerson">
  </div>

  <main>
    <strong>Contact</strong>
    <h2>Ralph Waldo Emerson</h2>

    <p>Unfortunately, Ralph Waldo Emerson has been deceased for over 100 years so he's not particularly expedient at replying to email but you can make an attempt below. However, if you require face-to-gravestone contact you can visit his remains at:</p>
    <address>
      <h4>Sleepy Hollow Cemetery</h4>
      <p>34 Bedford Street<br>
      Concord, MA 01742, United States<br>
    <a href="https://www.google.com/maps/place/Sleepy+Hollow+Cemetery/@42.464126,-71.343098,15z/data=!4m2!3m1!1s0x0:0x9c41d0f83df689a6?sa=X&ei=ZCgLVZb5Io_hoASc7oHwCQ&ved=0CH0Q_BIwCw">Google Map</a></p>
    </address>

    <form>
      <fieldset>
        <input type="text" placeholder="Full Name">
        <input type="email" placeholder="Email Address">
        <textarea placeholder="Your message..."></textarea>
      </fieldset>
      <input type="submit" class="button">
    </form>
  </main>

  <footer>
    <p>A project from <strong><a href="http://teamtreehouse.com">Treehouse</a></strong></p>
    <nav>
      <a href="index.html">About</a>
      <a  href="contact.html" class="selected">Contact</a>
    </nav>
  </footer>
</body>
</html>

Snapshot:

https://w.trhou.se/l6djegosad

This is my second attempt at this course. I've done all the other courses in the PHP track. I've enjoyed learning with Team Treehouse and completed 45 courses but I'm considering dropping this one due to disillusionment. I'd be grateful for any help.

3 Answers

Mark Railton
Mark Railton
8,468 Points

Looking at the provided snapshot, slim\views is not installed via composer. I have just forked this snapshot and tested running

composer require slim\views

then hitting the page and can confirm it works, when I removed slim\views it showed the same error you were getting, you just need to make sure that slim\views is installed via composer.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Mark. Thanks for your help. I typed composer require in the console and reinstalled slim\views. It's working now. If I can reciprocate, please let me know. :-)

Joshua Howard
Joshua Howard
12,676 Points

I have the same issue. Except i've tried reinstalling slim\views but still... no luck.

use updated slim\views version, not the version from the video which is 0.1 I think, not that one