How to kill apache while setting a session flash message PDF Print E-mail
User Rating: / 3
PoorBest 
CakePHP - Basics
Written by Stuart Duncan   
Thursday, 12 November 2009 16:45

I'm so new at this that I've been following along with text books and video tutorials trying to recreate what they do, learn from it.. modify it, put my twist on it, learn! But at this point, I was simply trying to copy a session flash message that I had seen my peer do in their tutorial and even though their results showed a very nicely formatted page, my results showed a spinning icon until eventually apache itself crashed. I checked, double checked, went through all of his and my files..... what did I do wrong??

 

Let's start with the basics before I spoil the ending of this story for you. If you're reading this, chances are you are just learning this session flash stuff as well so bare with me while I back track a bit to explain how I came to the problem and how I fixed it.

A session flash message looks like this:

$this->Session->setFlash('You have successfully added a post');

This line here is set in a controller and will set a message into a session to be displayed either further down the page, or if you redirect, on the next page that loads. Fairly simple, I can follow this. Have you ever seen this one?

$this->Session->setFlash(__('You have successfully added a post', true));

This one confused me for a while since I am not even sure how to phrase a search that would explain it to me but it turns out that the __() is commonly used as a localization function, meaning that CakePHP (or what ever script) will throw it into a dictionary blender and try to return the same phrase in another language if it can and the user so wishes it. But anyway, I digress...

In this tutorial that I was following along, the person had set a message with something looking like this:

$this->Session->setFlash('You have successfully added a post', 'success');

So I put the exact line into my code and hit the submit button and watched the cursor spin and spin and spin and then... error message. And not a thing would come up until I restarted apache!!

Here is where the "magic" of CakePHP stepped in. It turns out that the second argument in a setFlash method is to define a layout name. This allows you to have separate layouts for each message, so one for success and one for warning, essentially. If something fails, it shows your message in a different layout.

What they don't tell you is that if you don't have a file in your layouts called 'success.ctp', this setFlash method won't return a nice error message like it would if you were missing a controller or view. No, instead what it does is spin apache into oblivion.

CakePHP had always told me what I was doing wrong when I did it wrong up until that point, and never in my wildest dreams did I think a missing file would crash apache. And so for a long time I searched online, I read more books and tutorials and went crazy trying to figure out what in the world I did that was so different than this tutorial that I had just tried to replicate.

It was a missing 'success.ctp' file to match the second argument in the setFlash method.

 

Add comment


Security code
Refresh

Login to TyCamTech.com

Donate

Must have Pepsi, feed my addiction here:


Recommended Books

Introduction to Programming
A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way.

Setting up a Web Server
Teaches All The Steps To Hosting A Website From Home.

Learn PHP in 17 hours!
If you can copy and paste into HTML and you can follow simple instructions...

Follow us on



Follow tycamtech on Twitter