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 Contact Form & Sending Email Named Routes

Building Websites with PHP - Error due to Slim version difference on tutorial vs latest version installed

Please help to see how I could proceed with this code: Thank you! Because the $app = new\Slim\Slim( array( 'view' => new \Slim\Views\Twig()) is causing error that prevented webpage to be rendered... I tried changing it to what is in the Slim website as per below. However, this one also didn't work and shows a different error message :

Fatal error: Uncaught Error: Call to undefined method Slim\Route::name() in /home/treehouse/workspace/index.php:37 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 37

<?php require 'vendor/autoload.php'; date_default_timezone_set('America/New_York');

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

$app = new\Slim\App();

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

//Register component on container $container['view'] = function ($container) { $view = new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache'

]);

$view = $app->view(); $view->parserOptions = array( 'debug' => true );

$view->addExtension(new \Slim\Views\TwigExtension( $container ['router'], $container ['request']->getUri() ));

return $view;

}; $app->get('/', function() use($app){ $app->render('about.twig'); })->name('home');

$app->get('/contact', function() use($app){ $app->render('contact.twig'); })->name('contact');

$app->post('/contact', function() use($app){ $name = $app->request->post('name'); $email = $app->request->post('email'); $msg = $app->request->post('msg');

if(!empty($name) && !empty($email) && !empty($msg)) {

} else { //message the user that there was a problem $app->redirct('/contact'); }

});

$app->run();

3 Answers

Devin Gray
Devin Gray
39,261 Points

If you had the view working in the previous lessons I found a solution. You can read more about it on the slim framework website here:

http://www.slimframework.com/docs/features/templates.html

Instead of the method used in the video, you use the path_for() method to pass in the name of the page and what it's looking for. If you have the slim detailed error configuration setup you'll notice it doesn't recognize any of the functions listed on slim/views. I tried using the path() method and it asked if I meant to use the path_for() method and found out how it works. Below is the code I used for the links to make it work.

  <header>
    <h1>Ralph Waldo Emerson</h1>
    <nav>
      <a href="{{ path_for('about', { 'name': 'about' }) }}" class="selected">About</a>
      <a href="{{ path_for('contact', { 'name': 'contact' }) }}">Contact</a>
    </nav>
  </header>

This is how my index.php looks to display the view for Slim v3.

// Render Twig template in route
$app->get('/', function ($request, $response, $args) {
  return $this->view->render($response, 'about.twig', [
    'name' => $args['name']
  ]);
})->setName('about');

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

$app->post('/contact', function ($request, $response, $args) {
  var_dump($app->request->post());
});

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

Now I can click on the links and they direct me to the corresponding pages, although I haven't setup the selected class to change but I can always figure that out later. Hope this helps you out.

hi, Devin Gray I have made all required changes still my code is not working and showing "Slim application error" can you please help me with it

amadeo brands
amadeo brands
15,375 Points

Same problem here. Seems like the new version of Twig and the Slim view are not working as it sould. any one has a fix see my code down here:

Slim Application Error The application could not run because of the following error:

<!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="{{ baseUrl() }}" class="selected">About</a>
      <a href="contact">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="/}" class="selected">About</a>
      <a href="contact">Contact</a>
    </nav>
    {% endblock footer %}
  </footer>

</body>
</html>
Type: Twig_Error_Syntax
Message: Unknown "baseUrl" function in "main.twig" at line 30.
File: /home/treehouse/workspace/vendor/twig/twig/lib/Twig/ExpressionParser.php
Line: 573
Trace

#0 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/ExpressionParser.php(351): Twig_ExpressionParser->getFunctionNodeClass('baseUrl', 30)
#1 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/ExpressionParser.php(144): Twig_ExpressionParser->getFunctionNode('baseUrl', 30)
#2 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/ExpressionParser.php(84): Twig_ExpressionParser->parsePrimaryExpression()
#3 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/ExpressionParser.php(41): Twig_ExpressionParser->getPrimary()
#4 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Parser.php(144): Twig_ExpressionParser->parseExpression()
#5 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Parser.php(100): Twig_Parser->subparse(NULL, false)
#6 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Environment.php(615): Twig_Parser->parse(Object(Twig_TokenStream))
#7 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Environment.php(667): Twig_Environment->parse(Object(Twig_TokenStream))
#8 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Environment.php(396): Twig_Environment->compileSource('...', 'main.twig')
#9 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Template.php(286): Twig_Environment->loadTemplate('main.twig', NULL)
#10 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Environment.php(403) : eval()'d code(11): Twig_Template->loadTemplate('main.twig', 'about.twig', 1)
#11 /home/treehouse/workspace/vendor/twig/twig/lib/Twig/Environment.php(411): __TwigTemplate_87fa17a43c525a6dd65897900b8b7049d4e2de5230937e2da534aa1c8c9cd199->__construct(Object(Twig_Environment))
#12 /home/treehouse/workspace/vendor/slim/views/Twig.php(87): Twig_Environment->loadTemplate('about.twig')
#13 /home/treehouse/workspace/vendor/slim/slim/Slim/View.php(255): Slim\Views\Twig->render('about.twig', NULL)
#14 /home/treehouse/workspace/vendor/slim/slim/Slim/View.php(243): Slim\View->fetch('about.twig', NULL)
#15 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(757): Slim\View->display('about.twig')
#16 /home/treehouse/workspace/index.php(19): Slim\Slim->render('about.twig')
#17 [internal function]: {closure}()
#18 /home/treehouse/workspace/vendor/slim/slim/Slim/Route.php(468): call_user_func_array(Object(Closure), Array)
#19 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(1357): Slim\Route->dispatch()
#20 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#21 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#22 /home/treehouse/workspace/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#23 /home/treehouse/workspace/vendor/slim/slim/Slim/Slim.php(1302): Slim\Middleware\PrettyExceptions->call()
#24 /home/treehouse/workspace/index.php(27): Slim\Slim->run()
#25 {main}
Keith Doyle
Keith Doyle
25,973 Points

Same problem here.

Keith Doyle
Keith Doyle
25,973 Points

Actually, I just added this to my dependencies.php file and all is well. I never had the TwigExtension added before.

$container['view']->addExtension(new \Slim\Views\TwigExtension(
  $container['router'],
  $container['request']->getUri()
));