投稿日:

CakePHPで携帯サイトを作成する

前回携帯サイトを作った方法だと、CakePHPのバージョンアップによって動かなくなってしまっていたので、新しい方法で作った覚書を残していきます。
今回は「CakePHP1.2ガイドブック」の携帯サイトを作成するを参考に作っていっています。
この方法だとcakeのバージョンによって動かなくなることは無いと思いますが、一応バージョンは1.2.3.8166で試してみました。
内容はほぼそのままなのですが、自分が分かりやすいようにコメントを付け加えていってます。


1./config/routes.phpにprefixルーティンを設定する。

Router::connect('/m/:controller/:action/*', array('prefix' => 'mobile', 'mobile' => true));

urlで/m/コントローラ/アクションという感じにアクセスされるとコントローラーの「mobile_アクション名」が呼び出されるようになります。

2.Mobileコンポーネントを作ります
内容は次のような内容です。

class MobileComponent extends Object{
//prefixルーティンにmobileが入っていたらレイアウトにmobileを使うという処理を入れておきます
function beforeRender($controller){
if (!empty($controller->params['prefix']) && $controller->params['prefix'] == 'mobile'){
$controller->layout = $controller->params['prefix'];
}
}
//自クラスのgetUrl()関数を呼ぶ
function beforeRedirect(&$controller, $url, $status = null, $exit = true){
return MobileComponent::getUrl($controller->params, $url);
}
//prefixルーティンにmobileが入っていたらURLのGET情報にセッションIDをつけて/mでURLがなっていなかったら/mを付加しておく
function getUrl($params, $url){
if (!empty($params['prefix']) && $params['prefix'] == 'mobile'){
$sessionName = session_name();
$sessionId = session_id();
if (is_array($url)){
$url[$params['prefix']] = true;
$url['?'] = array($sessionName =>$sessionId);
}elseif(is_string($url)){
if (!preg_match("#^http[s]?://#", $url)){
$prefix = preg_match("#^/m/#", $url) ? '' : '/m';
$url = sprintf("%s%s?%s=%s", $prefix, $url, $sessionName, $sessionId);
}
}
}
return $url;
}
//prefixルーティンにmobileが入っていたら自クラスのconvertToInternal()関数を呼ぶ
function initialize(&$controller){
if (!empty($controller->params['prefix']) && $controller->params['prefix'] == 'mobile'){
MobileComponent::convertToInternal($controller->params);
}
}
//エンコーディングをUTF-8にして「半角カタカナ」を「全角カタカナ」に変換し、「半角」を「全角」に変換したのを返す
function convertToInternal(&$value){
if (is_null($value)){
return;
}elseif(is_array($value) || is_object($value)){
array_walk_recursive($value, array('MobileComponent','convertToInternal'));
}else{
$value = mb_convert_encoding($value, 'UTF-8', 'sjis-win');
$value = mb_convert_kana($value, 'KVa', 'UTF-8');
}
}
}

3.Mobileヘルパーを作ります
内容は以下の通りです。

class MobileHelper extends AppHelper{
//prefixルーティンにmobileが入っていたら 「全角」を「半角」に変換し「全角カタカナ」を「半角カタカナ」に変換しエンコーディングをShift_JISにする。
function afterLayout(){
if (!empty($this->params['prefix']) && $this->params['prefix'] == 'mobile'){
$view =& ClassRegistry::getObject('view');
$view->output = mb_convert_kana($view->output, 'rak', 'UTF-8');
$view->output = mb_convert_encoding($view->output, 'sjis-win', 'UTF-8');
header ("Content-Type: text/html; charset=Shift_JIS");
}
}
}

4.コントローラーでMobileコンポーネントとMobileヘルパーを呼び出しておきます

class XXXXXController extends AppController{
var $components = array('Mobile');
var $helpers = array('Mobile');
}

5.コントローラーで携帯用のアクションを作る
mobile_というアクション名にしてその関数内でPC用のアクションを呼んでおく

//--------------------------------------------------------------------------------
//一覧ページ
//--------------------------------------------------------------------------------
function index() {
//PC用の処理
}
//--------------------------------------------------------------------------------
//携帯用一覧ページ
//--------------------------------------------------------------------------------
function mobile_index() {
$this->index();
}

6./views/layoutに携帯用のテンプレートmobile.phpを作っておく。

7.コントローラーに合わせてviewsにmobile_の携帯用のviewを作っておく

8.app_helper.phpを作っておく
内容はviewで入力された$html->url(”)で入れられたURLを先に作ったMobileコンポーネントのgetUrl()を呼んで返ってきったURLに置き換える

App::import('Component', 'Mobile');
class AppHelper extends Helper{
function url($url = null, $full = false) {
return parent::url(MobileComponent::getUrl($this->params, $url), $full);
}
}