Continuing from my last post on choosing a php web development framework, here are some more tips on cakePHP that you may not otherwise come across as quickly
- if you change your database (as configured in app/config/database.php) make sure to delete the files in app/tmp/cache/models (see here)
- cakePHP will do some nice automation work for you if you create created and modified fields in your database tables. But for cake to do its magic, the default values of those fields have to be set to null, otherwise cake will assume they’re already set and won’t alter them
- if you need to set variables in your controller that will be accessible in your layouts, just append the string “_for_layout” to the variable name, and use Set as you normally would in the controller, e.g.
$this->set('whatever_for_layout', $variable); The variable will then be accessible in the layout as it would in a view, e.g.
<?php echo $whatever_for_layout; ?>
Having dabbled in ruby on rails but eventually given up for no better reason than that my web hosting provider (site5) is particularly difficult to get recent version of rails up and running (issues with rubygems and rails edge are attested in numerous places on the site5 forums), I set my sights on choosing a PHP web application framework. There are several reasons this makes better sense for me than rails:
- Learning more about PHP will probably better aid me in my other work than learning more about ruby, as the three most common content management systems (Joomla!, drupal, and wordpress) are all PHP
- support for PHP is much better on site5 than support for rails; support for PHP is in general much more common than support for rails
- PHP-based web app frameworks don’t require anywhere near the memory footprint required by rails apps, which leads to…
- … scaling issues? Actually, this isn’t really a concern. Most people have way bigger concerns than scaling like, say, getting their app to be popular enough that they have to worry about scaling. And while the spectre of Twitter and its many services outages that have been attributed to rails may be somewhat worrying, rails’ purported scaling difficulties are not foremost in my mind. There was a great comment on slashdot a while ago (wish I could find it…) that discussed how scaling problems can be greatly compounded by algorithm design and that, with well-designed algorithms most serious scaling issues can be nipped in the bud
So on to the PHP web app frameworks… What was I looking for in a framework? (for a comparison chart, see here)
- lack of bloat
- decent documentation
- significant developer/support community
- MVC architecture (does that go without saying?)
- decent templating system
- caching preferably
- ability to extend easily with other classes
- AJAX integration
- PHP 5 would be nice
To make a long story short, after a fair bit of comparison and review, I narrowed it down to cakePHP and CodeIgniter. Though both seem very strong, in the end I decided to go with cakePHP based mostly on this write-up by Chris Snook a respectable web developer and fellow Canadian.
As Chris writes, cakePHP does indeed make querying dead simple. And their philosophy of “convention over configuration” means that though it may take a little bit of getting used to (and some occasional forgetting of whether to camelCase or separate_by_underscores) the amount of file editing for configuration purposes is greatly reduced.
So far I’ve been very happy with cakePHP and have been able to get some fairly complicated apps up and running in under a day (cakePHP offers powerful scaffolding similar to rails), though I do have to agree that the documentation could still use some more work, and I find myself often having to compose obfuscated Google search queries for things that aren’t easily findable in the manual or API docs.
Some quick tips for those getting the hang of cakePHP (version 1.2):
- If you’re using UTF-8 collation in your database tables and web pages, you can make the necessary changes by adding the following line to your database.php configuration file:
'encoding' => 'UTF-8'
- don’t forget that just because you’re working with a copy of, say, a single record from your Posts model as $post in your views, you still need to reference fields like this:
- want to get rid of those SQL debug messages at the bottom of all your pages, and you tried changing the layout but they won’t go away? Edit /config/core.php and change the ‘debug’ from 2 to 0. You can also alter the debug level by controller method — see this post from the cakePHP Google group.
- a stupid mistake you may make too: if you’re trying to use php variable in a cakePHP findAll() function (e.g.
$this->Post->findAll('blog_id = $id AND language = "fr"'));) and are finding it doesn’t work, it’s because for variables to be parsed by PHP, they have to be enclosed in double quotes; otherwise, the above code will find all Posts that have blog_id equal to $id not whatever $id evaluates as. This may throw you off as all the examples in the cakePHP documentation make use of single quotes and no variables. So, in the above example, you would need
$this->Post->findAll("blog_id = $id AND language = 'fr'"));, a small but important difference
For some more tips on cakePHP, see here.