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 Views & Twig Inside of Slim

Stephen Garner
PLUS
Stephen Garner
Courses Plus Student 11,241 Points

Problems with differences between Slim v2.6 and current 3.2

I'm having some difficulty following along with these videos because the code to make these apps work is slightly different in the newer version. And I'm not aware of all the differences that occur, so I'm receiving Slim application errors whenever I go to preview my code.

Maximillian Fox
Maximillian Fox
Courses Plus Student 9,236 Points

I had this problem as well. It would be great if they did an updated version of this course with the new Slim 3.x features.

In composer, I believe you are able to specify the version that you would like to install for Slim. Try removing your current Slim install, and then install its previous version to follow along in the tutorials. Not the optimal solution as you are using an outdated version of Slim, but in order to complete the challenges etc it might be the only way for now.

Moises Cano
Moises Cano
2,261 Points

I feel ya. I am pulling my hair out trying to follow this tutorial and trying to search for answers on google for Slim 3. Why would I downgrade to v2 if I'm eventually gonna have to learn v3?

Steven Chan
Steven Chan
6,526 Points

I had the same problem that you had with the Slim v2 vs. v3. Anyway, after reading about some of the differences between the 2 versions, and lots of troubleshooting, the following code worked for me (I left out the monolog library for simplicity). It's pretty much from the slimframework website, although I had to change the templates directory to match the instructions from the speaker.

require '/vendor/autoload.php';

$app = new \Slim\App();

// Get container
$container = $app->getContainer();

// Register component on container
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig("templates/", [
        'cache' => false
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));

    return $view;
};

$app->get('/', function ($request, $response, $args) {
    return $this->view->render($response, "about.twig");
})->setName('profile');

$app->get('/contact', function ($request, $response, $args) {
    return $this->view->render($response, "contact.twig");
})->setName('profile');

// Run app
$app->run();
Yueh-Lin Lee
Yueh-Lin Lee
351 Points

I followed Steven's steps to finish it with Slim v3. Thanks, Steven.