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 trialPreston Yeschick
10,803 Points$app->get() function with given parameters not working
As I understand it, the first parameter of the $app->get() function is the URL extension that you want users to be able to type in, and the first parameter in the $app->render() function is the file that you want the page to route to (source from). I believe I assembled the function correctly but I'ts not working.
<?php
require 'Slim.php';
$app = new \Slim\Slim();
$app->get('/hello', function() use($app) {
$app->render('index.php');
});
/* add your code above this line */
$app->run();
4 Answers
Ted Sumner
Courses Plus Student 17,967 PointsI have always seen the require line to be this:
<?php
require 'vendor/autoload.php';
I don't know where you got Require 'Slim.php';
Atrian Wagner
24,811 Points"require 'Slim.php';" is part of the required exercise in in the Building websites with PHP module, which involves specifically using Slim.
Ted Sumner
Courses Plus Student 17,967 Pointshere is the end result of the code from that module.
<?php
// require Slim
require 'vendor/autoload.php';
// Set time zone
date_default_timezone_set('America/Los_Angeles');
//slim-views is required for Twig
//see https://github.com/slimphp/Slim-Views
//must install via composer
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true
);
//the code below enables us to use the helpers below
//urlFor siteUrl baseUrl currentUrl
//documentation at https://github.com/slimphp/Slim-Views
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$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)) {
$cleanName = filter_var($name, FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);
} else {
//message the user there was a problem
$app->redirect('/contact');
}
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance();
$message->setSubject('Email from Our Website');
$message->setFrom(array(
$cleanEmail => $cleanName
));
$message->setTo(array('teds@*******.net'));
$message->setBody($cleanMsg);
$result = $mailer->send($message);
if($result>0) {
// send a message that says thank you
$app->redirect('/');
} else {
// send a message to the user that the message failed to send
// log that there was an error
$app->redirect('/contact');
}
});
$app->run();
And here is the end result on a live site for a domain I own. It does not require Slim.php.
Atrian Wagner
24,811 PointsTed Sumner : What I mean is that the exercise this is from loads with "require 'Slim.php';" already as part of the code. The following is the code unaltered from the exercise.
<?php
require 'Slim.php';
$app = new \Slim\Slim();
/* add your code below this line */
/* add your code above this line */
$app->run();
Preston Yeschick
10,803 PointsIt turns out that in addition to using " require 'vendor/autoload.php'; ", I needed to remove the $app-> render line completely, as discovered in this community Q&A:
https://teamtreehouse.com/community/add-a-get-route-to-indexphp
Ted Sumner
Courses Plus Student 17,967 PointsThis code passed for me:
<?php
require 'Slim.php';
$app = new \Slim\Slim();
/* add your code below this line */
$app->get('/hello', function() use($app){
echo 'Hello There';
});
/* add your code above this line */
$app->run();
The first part of the challenge was to add $app->get('/hello');
. The second part added the remainder of the $app->get
line. I didn't have to remove anything from the code. I was just careful to add only what I was instructed to by the step of the challenge.
Anna Takang Nchongarrey-Oben
7,365 Pointsthanks a lot