CakePHP and the JSON Content Type
If you’re like me, you tend to make the same mistakes over and over. One simple one seems to get me each time I spin up a new project. We all know how easy CakePHP makes handling requests using Router::parseExtensions(). With that in place in your routes, Cake looks for the corresponding view based on the action called and responds accordingly.
After a touch more set up, appending ‘.json’ to the end of your url should result in a json response with the correct content-type header of ‘application/json’. If you find that you are getting ‘text/html’ as your content-type, you might have debug set to 1 or 2.
Hop on over to core.php (~/app/config/core.php) and check the value of ‘debug’ i.e. Configure::write(‘debug’, 0). If it is NOT set to 0, your response won’t be as expected.
Source: book.cakephp.org
Placeholder Text with a Sense of Humor
It’s no secret that when wire-framing, prototyping or developing, coming up with place holder text is a chore. For years, I relied heavily on http://lipsum.com. Straight up, no-frills lorem ipsum.
Thankfully, now there are no shortages of place holder text generaters out there. Why not have some fun the next time you need to fill up 4 paragraphs of copy? For me, its a tie between Hipster Ipsum and Samuel L. Ipsum as my favorites. But today via Twitter I was alert to Cupcake Ipsum and Bacon Ipsum. Mmmmmm bacon.
No longer will you waste time coming up with “asdfasdfasdfasdf”. Below is a taste of some Hipster Ipsum. Enjoy.
Food truck brooklyn Austin williamsburg echo park american apparel. Keffiyeh hoodie VHS wes anderson vinyl, mlkshk butcher locavore cliche. Wolf etsy readymade, ethical irony you probably haven’t heard of them food truck craft beer twee scenester cardigan brooklyn tattooed mustache photo booth. Gluten-free jean shorts tumblr quinoa readymade viral iphone, squid terry richardson.
IE Request Caching & AJAX
It’s true… I hate IE. The browser has caused nothing but headaches over the years and unfortunately, continues to do so. The lastest issue revolves around request caching. I have a simple ajax request that runs on the client side on a set interval checking for new content. For the most part, the requested URL does not change but that is not to say the results won’t.
The problem is IE, seeing that the URL has not changed, decides not to actually make the request. As a result, new content will never be returned. Thanks IE. Luckily, the fix is simple as you can see by the stackoverflow post I found. Just put a timestamp value on the end of your querystring. This way, each time the request is made, IE will think the URL is a new one.
For example:
var d = new Date();
$.get({url_endpoint}?v=+d.getTime(), function(data){
...
}
Source: stackoverflow.com
Beat the Bots
We’ve all had to do it… find a way to prevent spam bots from programmatically filling in web forms, and therefore your database, with junk. Ok, not all of us… but I have. Captcha, re-Captcha and the like are all fine solutions…. but they involve an extra step from the user. And many times, the words provided are unreadable increasing the time and effort to complete.
The other day I learned a trick. Within your web form, place a text input inside a hidden div so it is not visible to the user. Keep the value empty. On form submit, check to insure that this value is in fact empty. If for any reason, there is now data present, it is a safe bet a user is not submitting the form. Throw an error and beat the bots. Absolutely simple.
Granted, I have yet to put it into practice but I think it is a great, non-intrusive solve. Thanks for the tip Joel. You can read more here -> http://bit.ly/qViwN7
Source: joelpeterson.com
A Simple Empty Line
Fortunately, this one didn’t take too long to figure out…. but it definitely could have. While testing my “page not found” logic within a recent project, I noticed something was fishy. Even though the redirect was taking place, I was getting a “Warning (2): Cannot modify header information - headers already sent …”. Furthermore, instead of getting a helpful 404 in my headers, a happy 200 was being sent. Not good.
After poking around and a quick Google search, I came across my error. It turns out that I had a single blank line prior to my <?php block in one of my classes. This single line was enough to count as data being sent to the browser and therefore a warning to be displayed. A quick delete of this line… and a check in all my other classes, and all was good.
Hope this helps.