CakePHPでktai libraryを使う

  • このエントリーをはてなブックマークに追加

構成

いつもの通りバーチャルホストでテスト。

hosts

127.0.0.1       test.sample.com

vhost

#test.sample.com
<VirtualHost *:80>
    DocumentRoot "/home/sample/public_html"
    ServerName test.sample.com
    <Directory "/home/sample/public_html">
        AllowOverride All
        order deny,allow
        allow from All
    </Directory>
</VirtualHost>

ディレクトリ

http://sample.com/ ← ユーザー
http://sample.com/admin ← 管理画面
http://sample.com/mobile ← モバイル
と分けたい。

/home/sample/
		cake_core ← [vendors、cake、plugins]
		public_html ← [appのwebrootをコピー]
		cake_app ← [appをコピーしてuser,admin,mobileにリネーム]

index.phpの変更

public_html/index.php

/**
 * These defines should only be edited if you have cake installed in
 * a directory layout other than the way it is distributed.
 * When using custom settings be sure to use the DS and do not add a trailing DS.
 */
    if (PHP_OS == "WIN32" || PHP_OS == "WINNT") {
        define('C', 'C:');
    } else {
        define('C', '');
    }
/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
	if (!defined('ROOT')) {
		//define('ROOT', dirname(dirname(dirname(__FILE__))));
		define('ROOT', C.DS.'home'.DS.'sample'.DS.'cake_app');
	}
/**
 * The actual directory name for the "app".
 *
 */
	if (!defined('APP_DIR')) {
		//define('APP_DIR', basename(dirname(dirname(__FILE__))));
		define ('APP_DIR', 'user');
	}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		//define('CAKE_CORE_INCLUDE_PATH', ROOT);
		define('CAKE_CORE_INCLUDE_PATH', C.DS.'home'.DS.'sample'.DS.'cake_core');
	}

public_html/mobile/index.phpは[APP_DIR]を’mobile’に変更

Ktai Libraryをコピー

githubからパッケージをダウンロードしてappディレクトリをmobileにコピー。
Ktai Library本体をmobile/vendorsにコピー。

routes.php

Router::connectNamed(array(), array('argSeparator' => '~'));

ktai_app_controller.php

ktai_app_controller.phpをapp_controller.phpにリネームする。

$ktaiプロパティの追加

今回は各コントローラーに追加。
ソースファイルは全て、UTF-8で作成しcharset=Shift-JISとして出力とした。

    var $ktai = array(
        'input_encoding'            => 'UTF-8',
        'output_encoding'           => 'SJIS-win',
        'use_binary_emoji'          => true,
        'output_auto_convert_emoji' => true,
        'output_auto_encoding'      => true,
        //'output_convert_kana'       => 'knrs',
        'output_convert_kana'       => 's',
        //'use_xml'                   => true,
    );

shift-jisでデータがサブミットされるので、

    function beforeFilter () {
        parent::beforeFilter();
        mb_convert_variables("UTF-8", "SJIS-Win", $this->data);
    }

とした。

また、フォームの「accept-charset」はApp.encodingの値が設定されるので

<?php echo $this->Form->create('Regist', array('encoding' => 'shift-jis'));?>

とする。

最後に

app_error.phpでエラー処理しているのですが、Ktaiコンポーネントのshutdownメソッドが呼ばれずshift-jisで出力できないので、エラーページだけshift-jisで対応しました。

  • このエントリーをはてなブックマークに追加

SNSでもご購読できます。

ads

コメントを残す

*