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

How does routing work in V3 of Slim?

I don't quite understand how routing works in Slim V3. Will this video get an update?

Hi John,

sorry I don't have an answer for you, because I'm stuck with the same problem & considering reverting to slim 2!

Anybody know of any resources which can help us understand Slim V3 please?

Sergey Vrenev
Sergey Vrenev
28,778 Points

Same problem here. The docs are pretty straightforward, but i can't understand how routing and templates work in slim3.

--Update-- Guys, read this - https://teamtreehouse.com/community/php-slim-framework-30-new-update Helped a lot.

2 Answers

Following the steps the instructor provided, you'll install v3+ which means the steps he's guiding us through won't work. While it would be ideal if the videos were updated to teach the current version of SLIM & ensure that the steps he's guiding us through are accurate, who knows if and when that will happen. Until it does, you can install SLIM v2.6 to follow along:

  1. Delete your SLIM directory in your workspace
  2. Go to your console
  3. Enter "composer require"
  4. Search for package: SLIM
  5. Choose option 0 (Slim/Slim)
  6. Version Constraint: 2.6

Hi, here's the solution I've found so far (copy/paste of my answer in https://teamtreehouse.com/community/slim-application-error-message-please-help-slim-v32). It works, but it may not be the slimmest solution out there. Don't forget to install the 'slim/twig-view' package in Composer before running it. Here's my code:

<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/Berlin');

$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);

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

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

    return $view;
};

// Render Twig template in route
$app->get('/', function ($request, $response, $args) {
    return $this->view->render($response, 'index.html'
    );
});

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

$app->run();

Have a look at this page, that's where i took most of this code from, and it explains everything: http://www.slimframework.com/docs/features/templates.html

I hope this helps.