MainelyDesign.com Blog

A tale of caution: naming CakePHP "view" functions

Posted on 05/06/2009 at 05:13 pm by Kevin Wentworth
Viewed 14,359 times | 2 comments

Spoiler: functions that generate a view must be named using lower_case_with_underscores()

I just ran into a very interesting problem.  I was testing out some of my plugins to make sure they were still working with Cake version 1.2.2.8120.  They worked fine except for one function.  I had a form that updates a shopping cart, changing the quantities of products placed in the cart.  When I hit the submit button to change the quantities, I was getting redirected to my site root. 

The first thing I did was put a simple echo command at the top of the function and a die(); right after that.  This is my favorite-first-way of telling what's going on. I noticed that I could comment out the call to beforeFilter() and my echo command would work.  I moved my die(); statement all the way up the beforeFilter chain and couldn't find any place that was redirecting before my die(); call.  I knew it wasn't something I coded (first thought).

The next thing I did was create a dummy function with almost the same name.  That worked!  I copied my function and added some characters after the name to make a new function... redirected to the site root.  What gives?  Finally I had a thought- the one that worked wasn't small-capped, while the function that was redirecting was named in small-caps.  I did a quick search and found this tidbit that reinforced my thinking:

"For functions that are used as views pages, use underscores to break words. Also use all lower caps." - source

What this means is that functions that are ultimately views MUST be named in lower_case_with_underscores!  For whatever reason, my function named updateQuantities() worked since 1.2.6311 beta up until the release of 1.2.2.8120.  I'm glad I found that post.

Happy coding!  Cheers,

-Kevin Wentworth

Bookmark and Share

Tags for A tale of caution: naming CakePHP "view" functions

Cakephp | Upgrade | Site Avenger | Web Programming

Comments for this Posting

Posted by vafReepay

on 16/2/11

Amazing. Love this nice thought. Please also check out the last user products and services reviews, including the most requested bali villas review.

http://www.bali-villas.net

Posted by ipod to mac transfer

on 16/2/11

Hello.
I think either your system is too slow or your having a heating problem

blu ray to dvd converter

http://www.ipod-to-mac.net/

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design