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 trialMohammed Khalil Ait Brahim
9,539 Pointsfinishing the project for the new build a website with PHP course ( attempt )
Dear all,
The following is more of a blog than a pure question. I am writing this to illustrate the way I finished the project we started with Hampton.
First of all, Let's solve the issue with header and footer. As you may have noticed the header doesn't highlight the right section on the contact page. To solve this issue, I declared a variable called page in the twig template files of each page (not the main.twig) and I used that variable as an indicator to know which link should be selected
The following is in my about.twig a similar snippet is in my contact.twig :
{%
set page = "home"
%}
{% extends 'main.twig' %}
// The remaining block
This is what happens in my main.twig header ( same for footer ):
<header>
<h1>Ralph Waldo Emerson</h1>
<nav>
<a href="{{ urlFor('home') }}" {% if page == 'home' %} class="selected" {% endif %}>About</a>
<a href="{{ urlFor('contact') }}" {% if page == 'contact' %} class="selected" {% endif %}>Contact</a>
</nav>
</header>
With this working properly, I moved to solve the email notification status to do that I learned about flash messages in Slim ( I used code from documentation to start a session though I didn't fully understand what was happening ). Here is the code to start a session :
<?php
// Code to instantiate all the object here
$app->add(new \Slim\Middleware\SessionCookie(array(
'expires' => '20 minutes',
'path' => '/',
'domain' => null,
'secure' => false,
'httponly' => false,
'name' => 'CookieSession',
'secret' => 'CHANGE_ME',
'cipher' => MCRYPT_RIJNDAEL_256,
'cipher_mode' => MCRYPT_MODE_CBC
)));
// code to handle the post goes here
?>
Now that I started a session let's send a message to know whether email was sent successfully or no again documentation was a great inspiration :
<?php
$app->post('/contact', function () use($app) {
$name = $app->request->post("name");
$email = $app->request->post("email");
$message = $app->request->post("message");
$ok = !empty($name) && !empty($email) && !empty($message);
if( $ok ) {
$cleanName = filter_var($name, FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($message, FILTER_SANITIZE_STRING);
} else {
// message the user that there was a problem
$app->flash('info', 'fail');
$app->redirect($app->urlFor('contact'));
}
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
$mailer = \Swift_Mailer::newInstance($transport);
$msg = \Swift_Message::newInstance();
$msg->setSubject("Email From Website");
$msg->setFrom(array(
$cleanEmail => $cleanName
));
$msg->setTo(array('khalilwtf@gmail.com'));
$msg->setBody($cleanMsg);
$res = $mailer->send($msg);
if(!$res) {
// LOG ERROR MESSAGE
$log->addError("Email Couldn't be sent");
$app->flash('info', 'fail');
$app->redirect($app->urlFor('contact'));
} else {
$app->flash('info', 'success');
$app->redirect($app->urlFor('home'));
}
});
?>
What is left to do now is to check whether the variable in flash are defined when we load a page and I did it using twig if statements pretty straightforward :
<body>
{% if flash['info'] == "success" %}
<div id="feedback" class="success">
<h3>Success!</h3>
<p>You're reading all about Emerson.</p>
</div>
{% endif %}
{% if flash['info'] == "fail" %}
<div id="feedback" class="fail">
<h3>Fail!</h3>
<p>There was an error with the email! Try Again</p>
</div>
{% endif %}
I hope you enjoyed reading and would like to hear your comments on the way I solved this problem and what are things I can improve or even how you guys tackled it
:D
Gary Alan Jackson
14,014 PointsThank you! I had all the pieces, but got stuck on where to put them. :)
Michael Paulmeno
6,373 PointsMichael Paulmeno
6,373 PointsThank you very much!