A fusion of technology, music, and geekyness.

CakePHP Character Encoding Checklist

I was having some issues today with character encoding and I thought I’d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn’t hurt either.

1. Database Config

Add ‘encoding’ => ‘utf8′ to app/config/database.php

	var $default = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'database',
		'encoding' => 'utf8'
	);

2. Make sure your layout has the charset line in the

	<head>
	 	<?php echo $html->charset(); ?>
	 	Other elements...
	</head>

3. This one is set by default, but make sure App.encoding is set in app/config/core.php

/**
 * Application wide charset encoding
 */
	Configure::write('App.encoding', 'UTF-8');

And thats it!

5 Responses to “CakePHP Character Encoding Checklist”

  1. Thales says:

    Thumbs Up. Thanks for you post. ;)

  2. someone says:

    This was the trick

    “‘encoding’ => ‘utf8′”

    Thanks!

  3. Sérgio says:

    ‘encoding’ => ‘utf8′
    solve my problem to =)
    Thanks!

  4. Jonathan says:

    For #2, will that need to be in each view? Still pretty new to cakephp and my app causes funny characters to be put into the database when another language is used.

  5. Jonathan says:

    I figured it out, it was #1. Thank you for the helpful page!

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes