I need to implement a “mobile browsing experience” in addition to the “large-screen browser experience” for my site. I’m looking for the appropriate Yii pattern to implement this. Is a mobile theme the Yii way to do this or something else? Assuming I use the library mentioned in this post to detect the client type, where do I apply the logic to direct the request?
Thanks for the help. Here’s what I did. Feedback welcome.
I already have a slightly modified configuration setup that has two extra config files, conditional.php and local.php. The conditional.php file sets config elements according to global constants defined (e.g. YII_DEBUG) in the main.php, and local.php sets values according to my local environment. Both add to or overwrite values set in the main.php config file.
My index.php is pretty much as it was generated. My protected/config/main.php looks like this:
<?php
$config_array = array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My App',
// ...
);
// These may overwrite anything in $config_array
require('conditional.php');
require('local.php');
return $config_array;
?>
And my protected/config/conditional.php looks like this:
<?php
// Conditional config params and PHP inits
if (defined('YII_DEBUG')) {
error_reporting(-1); // All possible errors
ini_set('display_errors', '1');
$config_array['components']['log'] = array(
'class'=>'CLogRouter',
'routes'=>array(
// ...
),
);
} else {
}
// Theme Selection Based On Browser Detection
$extDir = $config_array['basePath'].DIRECTORY_SEPARATOR.'extensions';
$clientInfoClass = 'Browser';
$clientInfoPath = $extDir.DIRECTORY_SEPARATOR.
'wx_clientinfo'.DIRECTORY_SEPARATOR.$clientInfoClass.'.php';
require($clientInfoPath);
$clientInfo = new $clientInfoClass;
if ($clientInfo->isMobile()) {
$config_array['theme'] = 'mobile';
}
?>
I am using Chris Schuld’s Browser.php implementation for browser detection though there are others (see this topic). I put the Browser.php file in my arbitrarily named extension directory: protected/extensions/wx_clientinfo.
For the time being I do not need to access the Browser instance once I’ve made my theme selection, so I am not adding it as a Component or even as an application parameter.