Oct 23 2008

Using UTF8 In Your CakePHP App

Published by Justin at 9:31 am under CakePHP, CakePHP tips

Your correspondent has been heavily developing a side-project for the last month in CakePHP, a MVC, RoR-inspired framework for building web applications in the dreaded language PHP.

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'
);

18 responses so far

18 Responses to “Using UTF8 In Your CakePHP App”

  1. stockholmon 15 Dec 2008 at 7:24 am

    you.. really saved my life. damn, I’m from germany and we got those nasty little umlauts. It took around 2 f*cking hours to find out, that umlauts aren’t supported the standard way in cakephp… I just started hating all the damm framework but you guy, you saved my life.

    Just a few keywords for everyone, who is searching the same solution as I was, till I found yours:

    CakePHP, Umlauts, Umlaute, Forms, Message, FormHelper

    Thank you so much,

    stockholm

  2. Justinon 15 Dec 2008 at 8:23 am

    @stockholm – Glad I could help! Keep the faith, cake rocks.

  3. Georgeon 07 Jan 2009 at 11:05 am

    Thank you very much. I absolutely love how you expressed your frustration :) It is like you were there when I was talking to myself the last couple of hours. I Like your writing style as well. Keep it up.

  4. Vladon 27 Apr 2009 at 3:45 pm

    You are really great….
    Helped me a lot with special romanian characters…….

  5. Druon 28 Apr 2009 at 7:18 pm

    You rock! I’m coming from RoR’s and I am required to use PHP for this project so CakePHP it is. You have saved me big time. Thank You! Oh, and if you didn’t get the first one, Thank You!

  6. Justinon 28 Apr 2009 at 8:31 pm

    @Dru – Happy to help.

  7. Sebastienon 28 May 2009 at 5:56 am

    I’m keep having those “diamonds question marks” on letter like… é É À Ç ….grrrrr

  8. Sebastienon 28 May 2009 at 6:00 am

    Ok… I found what I made wrong…. was my default.po (language file) who was encoded in ISO-8839-1… hehe… thanks a lot buddy

  9. Boomeron 09 Jul 2009 at 1:50 pm

    Nice! I had some issues with UTF-8, but now they’re gone. BTW, do you know if there’s another special procedure for Oracle? I’m starting some projects that require it, and the databases are already created, so there’s no option there…

    Thanks and keep it up!

  10. Justinon 09 Jul 2009 at 2:03 pm

    @Boomer – Not really familar with Oracle. I might try posting to StackOverflow.com

  11. Julienon 03 Aug 2009 at 6:29 am

    Thanks a lot for this post!

  12. Nehaon 29 Aug 2009 at 12:25 pm

    Thanks a lot. Really helpful post.
    ‘encoding’ => ‘utf8′ was something I couldn’t find in the documentation.

  13. Tomon 03 Sep 2009 at 11:48 am

    Brilliant! Thank you very, very much! I really wonder why those fundamental things are not in the Cake docs.

  14. Logic Lab Posting 1 « LogicBomb Mediaon 03 Nov 2009 at 10:31 am

    [...] The short explanation: Set everything to UTF-8 at the beginning of your project. The long explanation (which is just a rehash of this awesome post at missingfeatures.com): [...]

  15. steveon 05 Nov 2009 at 9:05 pm

    Fantastic! Your last “little gem” with adding the utf encoding to the database configuration is the last little piece that I’ve been looking for — for a LONG TIME NOW!! I had resorted to using
    Configure::write(‘App.encoding’, ‘ISO-8859-1′)
    in core.php, but now I’m all UTF8, all of the time!

    Thanks for the tip!

  16. mishuon 26 Jan 2010 at 6:20 am

    Hi man,
    I have a problem displaying some hungarian chars although the document is declared UTF 8 and in database.php I added the encoding line. The problem is that when I use “&#336 ;” – with no spacing – (which is the Ő or the O with two points above) in default.po the output is the actual code not the character. Any ideas how this can be sorted? I’d really appreciate an email answer since you got no “get comments on email” checkbox. Thanks mate.

  17. Justinon 26 Jan 2010 at 9:16 am

    @mishu – I emailed you this, but for completeness, here is one ideas to try:

    &#336; needs to be output as Ő for HTML to read it as an encoded character (in the source code of output HTML file). Try passing your output through html_entity_decode – http://www.php.net/manual/en/function.html-entity-decode.php.

  18. Mailson Liraon 20 Feb 2010 at 8:59 am

    I cannot believe that I spent the last 3 hours trying to solve this.
    Just one line, and problem solved!

    Thank you!

Trackback URI | Comments RSS

Leave a Reply