CakePHPでユーザーエージェントにより、デザインを変更します。
デザインを変更するには様々な方法がありますが、今回はテーマを使用し、Viewsフォルダにフォルダを追加し対応します。
概要
使用するファイルは『app/app_controller.php』と、viewsに追加するテンプレートになります。
app/app_controller.phpを変更する
『app/app_controller.php』を編集し、ユーザーエージェントによりテンプレートを変更するようにします。
今回はCakePHPのThema機能を使用し、Viewsフォルダの中のフォルダは『sp・default』にします。
class AppController extends Controller {
public $view = 'Theme';
public function beforeFilter() {
if ($this->InsAuth) {
$this->InstructorAuth();
}
if ($this->AdminAuth) {
$this->AdminAuth();
}
if ($this->FreeAuth) {
$this->FreeAuth();
}
$useragents = array(
'iPhone', // Apple iPhone
'iPod', // Apple iPod touch
'Android' // Android
);
$pattern = '/'.implode('|', $useragents).'/i';
if($_ua = preg_match($pattern, $_SERVER['HTTP_USER_AGENT'])){
$this->theme = "sp";
}else{
$this->theme = "default";
}
}
}
とbeforeFilterを作成し、記述します。
これで、viewsフォルダに『sp・default』フォルダを作成するとこちらを読みに行くようになります。
