2011年3月9日水曜日

[PHP] cakephp+sencha-touchでiphoneWebアプリ開発<序章その2>

セッションの件はわからないけれど、携帯用にSJIS出力する際のメモ

基本文字コードはUTF-8で統一ですが、一部ガラケーはUTF-8に対応していないので、Shift-JISに変換する必要があります。



app_controller.php(自分の場合はすべてのcontrollerの基底クラスとして定義) 内に以下のメソッドを定義します。




function beforeRender() {
parent::beforeRender();
if ($this->RequestHandler->isMobile()) {
if ($this->__isSmartPhone()) {
$this->viewPath = 's';
$this->layoutPath = 's';
} else {
$this->viewPath = 'm';
$this->layoutPath = 'm';
Configure::write('debug', 0);
$this->RequestHandler->respondAs('html', array('charset' => 'Shift_JIS'));
}
}
}



※beforeRender内ではレイアウトパスの変更と、文字コードヘッダの指定を(デバッグモードを切っておかないと面倒なようです)

この場合だとviews/layout/s/内にスマホのviews/layout/m/内にガラケー用のレイアウトファイルを配置します


function afterFilter()
{
parent::afterFilter();
if ($this->RequestHandler->isMobile() && !$this->__isSmartPhone()) {
//header("Content-type: text/html; charset=Shift_JIS");
//Configure::write('App.encoding', 'Shift_JIS');
$this->output = mb_convert_kana($this->output, 'rak');
$this->output = mb_convert_encoding($this->output, 'SJIS-win', 'UTF-8');
//$this->RequestHandler->respondAs('xhtml', array('charset' => 'Shift_JIS'));
}
}



※afterFilter内では出力するhtmlの文字コードを変換します。

後は送信データのコンバートをしないといけない(追記予定)

0 件のコメント:

コメントを投稿