Common CakePHP Hurdles: Naming Conventions
Recently, I’ve been helping one or two people with some CakePHP questions. I’m happy to help these folks get past issues I remember confounding me back when I first started developing with the framework, and I’ve had a couple requests to write some blog articles about it.
One of the regular issues I keep tripping over when developing with CakePHP is the naming conventions. Even years later and dozens of CakePHP applications under my belt, I can still lose hours being frustrated at an obscure bug that turns out to be caused by a simple typo.
The general idea is that datatables, files, and classes should all be named a very specific way. If done right, not only will CakePHP be able to auto-magically link these associated items together, its built-in functionality will set up as many of the options and parameters for you. This can save a developer a lot of time and coding. However, if set up incorrectly, one of two things will happen.
One, the app cannot work, and if you look at the debug information,CakePHP will give you as much information as it can. Using the provided debug tools, settings, reading the provided notifications and error messages, and going through any logs are invaluable.
Two, CakePHP will use auto-magic to work around your link and not tell you it’s doing so. This is the point where debugging can be difficult if you don’t know what you’re looking for.
The issue most commonly comes when a file is misnamed. In my case, it tends to be a model file. CakePHP will recognize a call to a model but not be able to find the model file, so it will create a model class in memory under the name it is looking for. If it then finds the datatable correctly, it continues on it’s merry way. This auto-magically created model class is pretty bare bones, however, and will not include any customization the actual model file has, like relationships, attributes, methods, etc. So if the developer gets errors, the most prominent ones will be about a relationship or attribute and the like not being available... but when he checks his file, they are clearly there.
So, if you seem to be unable to access methods that have no syntax errors or there seem to be attributes with the wrong values in them despite you declaring them otherwise, check to make sure all your files are actually being used like you think they are. Check that file names, folder names, class names, and datatable names follow CakePHP’s naming conventions. Throw in pr() functions and exit() functions to display on screen that those files and functions are actually be used or set. Check file and folder permissions to make sure they are accessible.
Also, if you are working with plugins, make doubly sure you are accessing the files from the right place. Another common trip up I had early on was having CakePHP trying to access a class from the root app folders rather than the plugins’ folders.
Finally, I recommend keeping a cheat sheet handy, such as this one or this one. They may be out of date by a version or two as of this writing, but it should at least get you looking in the right direction if something comes up.
Hope this helps :)