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

Given example loads empty page on local developing system

I've set up an Apache server and PHP on my local system, the first examples in this tutorial work well (e.g. "/hello/peter/" outputs "Hello Peter").

When I try to put the index.html and contact.html in the "templates folder" (as explained in this tutorial) and try to access the websites in my browser (http://dev1.local/contact or http://dev1.local/), an empty page is loaded instead of the expected index and contact pages I set up like in the tutorial.

Here are some further infos for my setup:

index.html:

  1 <?php
  2 require __DIR__ . '/vendor/autoload.php';
  3 
  4 $app = new \Slim\App;
  5 
  6 $app->get('/', function() use($app){
  7   $app->render('index.html');
  8 });
  9 
 10 $app->get('/contact', function() use($app){
 11   $app->render('contact.html');
 12 });
 13 
 14 $app->run();

apache config:

<VirtualHost dev1.local:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/dev1
        ServerName dev1.local
        <Directory "/var/www/dev1">
          AllowOverride All
          Allow from All
        </Directory>
</VirtualHost>

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ index.php [QSA,L]

composer packages installed:

''' ./composer.phar show installed: container-interop/container-interop 1.1.0 monolog/monolog 1.17.2 nikic/fast-route v0.6.0 pimple/pimple v3.0.2 psr/http-message 1.0 psr/log 1.0.0 slim/slim 3.0.0 slim/views 0.1.3 twig/twig v1.23.1 '''

I can't find the issue. Help would be very much appreciated.

Adam Pengh
Adam Pengh
29,881 Points

A blank page in PHP generally indicates some type of syntax error. You should try editing your php.ini configuration file to allow errors to be displayed on the page.

luciano c.niell
luciano c.niell
6,367 Points

Im having the same issue, not sure what's going on.

I have set

display_errors = On
error_reporting = E_ALL

in my apache php.ini.

The page is loaded empty, there is no sign of an (obvious) error.

I've tried loading the website with the php built-in webserver ("php -S localhost:8000"), I get the same there. It can be concluded it's not an Apache config problem - the error lies in the code somewhere.

William McManus
William McManus
14,819 Points

I have the same issue, I found that when I put the index.php file in the templates directory, and move the index and contact.html files to the main directory it works fine. Which is very, very strange. I am sure the problem is somewhere in here:

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

'''

But I don't know what it is either. Maybe a version conflict in Slim 3.0?

This is most definitely due to the changes in Slim 3.0. For example, render() can no longer be called on $app, but now must be called on the view object. Here is a link to a comment that another student made on this subject.

https://teamtreehouse.com/community/php-slim-framework-30-new-update

There is a little more work that goes into targeting the templates folder. I still haven't figured out how to include contact.html, but I have been able to get index.html to load. Here is my code so far:

<?php
// From Slim 3
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

// Use Composer's autoload feature to autoload vendors
require '/vendor/autoload.php';

// Create new Slim app
$app = new \Slim\App();

// Create container that we'll use to point to templates 
$container = $app->getContainer();

// Register Twig-view component in 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;
};

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


// This doesn't work just yet
$app->get('/contact', function (Request $request, Response $response) {
  return $this->view->render($response, 'contact.html');
});

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

?>

Be sure to install Slim 3.0's Twig component with this: composer require slim/twig-view

Pimple, which is "a PHP dependency injection container", is already part of your Composer application. It provides you with the ability to create a container for your template folder contents. Twig is "a template engine that compiles templates into plain optimized PHP code." Pimple and Twig can be used to create a container of your HTML files that have been compiled into PHP code to be rendered by your get method.

If anyone can tell me why I can't render contact.html yet, please let me know. Moving on to the next video for now.