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

Okafor Matthew
Okafor Matthew
Courses Plus Student 2,633 Points

include and rendering

Help Please, i don't know what i have done wrong, i followed the teacher's instruction then after i render an html file i got an error such as Slim Application Error

The application could not run because of the following error: Details Type: RuntimeException Message: View cannot render template/index.html because the template does not exist File: /home/treehouse/workspace/vendor/slim/slim/Slim/View.php Line: 272 Trace

0 /home/treehouse/workspace/vendor/slim/slim/Slim/View.php(255): Slim\View->render('template/index....', NULL)

1 /home/treehouse/workspace/vendor/slim/slim/Slim/View.php(243): Slim\View->fetch('template/index....', NULL)

2 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(755): Slim\View->display('template/index....')

3 /home/treehouse/workspace/index.php(10): Slim\Slim->render('template/index....')

4 [internal function]: {closure}()

5 /home/treehouse/workspace/vendor/slim/slim/Slim/Route.php(468): call_user_func_array(Object(Closure), Array)

6 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(1355): Slim\Route->dispatch()

7 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call()

8 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()

9 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()

10 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(1300): Slim\Middleware\PrettyExceptions->call()

11 /home/treehouse/workspace/index.php(17): Slim\Slim->run()

12 {main}

Darrell Conklin
Darrell Conklin
21,988 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.

1 Answer

Aleksandr Antropov
Aleksandr Antropov
6,458 Points

If you want to use slim v3:

  1. Install slim/php-view
  2. Your code should look like this:

use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response;

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

$app = new \Slim\App;

$container = $app->getContainer(); $container['view'] = new \Slim\Views\PhpRenderer('templates');

$app->get('/', function(Request $req, Response $res, $args = []) { $res= $this->view->render($res, 'index.html'); return $res; });

$app->get('/contact', function(Request $req, Response $res, $args = []) { $res= $this->view->render($res, 'contact.html'); return $res; });