Oct 23 2008
Overall, it’s a great framework. There are some sore points (documentation is solid but there are lots of gaping holes), but overall your correspondent is confident that any PHP programmer who uses CakePHP for the first time will quickly be in the I’m-never-doing-PHP-again-unless-I-have-some-kind-of-framework camp. Seriously, never again.
Still, there were some things your correspondent wished he knew when he started. This will be the first is a series of posts on CakePHP tips for newbies.
CakePHP Tip: Start in UTF, everywhere
Character encoding sucks but you kinda have to know about it. To save yourself pain, just start on day one in UTF8 and there won’t be any encoding pain down the line.
First, add this to your layouts to have your pages output an HTML header that screams to the browser I’m fucking UTF, OK?. Put this line at the top of your layout (before any other output):
<?php header('Content-type: text/html; charset=UTF-8') ;?>
Just in case the first message fell on deaf browser ears, add this inside your HEAD tags as a backup:
<?php echo $html->charset('utf-8'); ?>
Then, make sure you set your MySQL collation to utf8_general_ci before you start building your database. If you started your database in some kind of latin collation, sucks to be you. You will have to update the collation manually in every table and every column that is text, varchar or char.
Lastly, there is one more little gem you need to do. You need to set the encoding on your CakePHP MySQL settings to utf8 to ensure that when Cake talks to MySQL, it’s using UTF. In your config/database.php file add this line as the last property for each connection:
'encoding' => 'utf8'
So each database connection looks something like this:
var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'username', 'password' => 'password', 'database' => 'dbname', 'prefix' => '', 'encoding' => 'utf8' );