はじめに
CakePHPでモバイルとPCのデザインを分離していきます。
UserAgentで端末を判定
判定処理をComponentに実装します。
app/Controller/Component/MyRequestHandlerComponent.php
<?php App::uses('RequestHandlerComponent', 'Controller/Component'); class MyRequestHandlerComponent extends RequestHandlerComponent { public function isSmartPhone() { $ua = env('HTTP_USER_AGENT'); return ((strpos($ua,'iPhone')!==false) || (strpos($ua,'iPod') !== false) || (strpos($ua,'Android') !== false)); } }
次に、全コントローラーのインスタンスを生成する際に、スマホかどうかをチェックします。
モバイルテーマと設定してあげると、/View/Event/add.ctp ではなく /View/Themed/Mobile/Event/add.ctp となります。
app/Controller/AppController.php
App::uses('Controller', 'Controller'); App::uses('AuthComponet', 'Controller/Component'); class AppController extends Controller { public $components = array( 'Auth'=>array(), 'RequestHandler' => array('className' => 'MyRequestHandler') ); public function beforeFilter() { $this->log('[AppController/beforeFilter()] REQUEST', 'debug'); $this->Auth->allow(); if($this->RequestHandler->isSmartPhone()) { $this->theme = 'Mobile'; } } }
画面確認
FireMobileSimulatorというUA変更ツールがあります。
それをChromeに入れて、iPhoneやAndroidの画面で開発を進めましょう。
参考
http://weble.org/2012/04/30/ca……ery-mobile
http://www.polidog.jp/2013/07/……定する/