Fawad Ghafoor ( - https://web.archive.org/web/20110225213957/http://www.web-punk.com/2010/03/zend-framework-applications-for-iphone-blackberry-co/)
, , - .
Zend Framework iPhone, BlackBerry Co
. , , iPhone, BlackBerry ..
: , , - - MVC (//) , , Zend_Layout.
1.
, , , - , - : a) , b) V MVC ).
. , : (Zend_Layout), -, (Zend_Translation). , , ? , , , ( looong) ( , , ..)
, Zend Frameworks (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html) - , ... ContextSwitch , " ". : JSON XML. "mobile".
... - : http://www.web-punk.com/wp-content/uploads/2010/03/mobile_wf-300x94.png
, , " " ( , ). http://mobile.example.com/controller/action, "". http://www.example.com/controller/action, , . , , -. , , , / ( , ).
, , http://www.qulpa.com
2.
. , , . , WURFL. , true, false . , , . , .
. \plugins Mobile.php :
<?php
class Plugin_Mobile extends Zend_Controller_Plugin_Abstract
{
private $ask_again_after_x_minutes = 10;
private $test_mobile = 0;
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
if (isset($_SESSION['mobileasked'])) {
if (time() < $_SESSION['mobileasked']) {
$_SESSION['mobileasked'] = time() + $this->ask_again_after_x_minutes * 60;
return $request;
}
}
if ($this->checkmobile() || ($this->test_mobile == 1)) {
if (!(Zend_Registry::isRegistered('directmobile') && Zend_Registry::get('directmobile') == '1')) {
$_SESSION['mobileasked'] = time() + $this->ask_again_after_x_minutes * 60;
$request->setControllerName('index')
->setActionName('askmobile')
->setParam('format', 'mobile')
->setParams($request->getParams())
->setDispatched(false);
}
}
return $request;
}
private function checkmobile(){
if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) return true;
if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) return true;
if(isset($_SERVER["HTTP_USER_AGENT"])){
$badmatches = array("OfficeLiveConnector","MSIE\ 8\.0","OptimizedIE8","MSN\ Optimized","Creative\ AutoUpdate","Swapper");
foreach($badmatches as $badstring){
if(preg_match("/".$badstring."/i",$_SERVER["HTTP_USER_AGENT"])) return false;
}
$uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos");
foreach($uamatches as $uastring){
if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) return true;
}
}
return false;
}
, ! - :
$loader = new Zend_Loader_PluginLoader(array(
'Plugin' => APPLICATION_PATH . '/application/controllers/plugins',
));
$pluginList = array(
'plugin1' => $loader->load('Plugin1'),
'plugin2' => $loader->load('Plugin2'),
'mobile' => $loader->load('Mobile'),
);
$frontController = Zend_Controller_Front::getInstance();
foreach ($pluginList as $pluginClass) {
$frontController->registerPlugin(new $pluginClass());
}
3.
, . , , , . . bootstrap.php - :
$domains = explode('.', $_SERVER['HTTP_HOST']);
if ($domains[0] == 'mobile' || $frontController->getParam('format') == 'mobile') {
if ($domains[0] == 'mobile') {
Zend_Registry::set('directmobile', '1');
}
Zend_Registry::set('context', '\mobile');
} else {
Zend_Registry::set('context', '');
}
4.
, , . / (. 2).
IndexController.php askmobileAction:
public function askmobileAction()
{
}
... , ;-). . views, , , \views\scripts\index, askmobile.mobile.view - :
How do you want to use this application?<br/>
<a href="<?= 'http://mobile.fopp.de' . $_SERVER["REQUEST_URI"]?>">MOBILE VERSION</a>
<br></br>
<a href="<?= 'http://dev.fopp.de' . $_SERVER["REQUEST_URI"] ?>">DESKTOP VERSION</a>
, ? , . , ContextSwitch , name.MOBILE.phtlm name.phtml, MOBILE.
5.
- . , , . .
\layouts \mobile ( , , \application\layouts\mobile, ). layout.phtml - :
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php echo $this->headTitle() . "\n" ?>
<?php echo $this->headLink() . "\n" ?>
</head>
<body>
<div id="header">This is your header</div>
<div id="content"><?= $this->layout()->content ?></div>
<div id="footer">This is your footer</div>
</body>
</html>
. , , , , , CSS ..
6.
. - . init() IndexController, . , OO-Design (, MyMobileController, Zend_Controller_Action .ini), .
IndexController.php : /** * * * @return void */ public function init() { :: INIT();
if (Zend_Registry::get('context') == '\mobile' || $this->getRequest()->getParam('format') == 'mobile')
{
$mobileConfig =
array(
'mobile' => array(
'suffix' => 'mobile',
'headers' => array(
'Content-type' => 'text/html; charset=utf-8')),
);
$contextSwitch = $this->_helper->contextSwitch();
$contextSwitch->setContexts($mobileConfig);
$contextSwitch->addActionContext('index', 'mobile');
$contextSwitch->addActionContext('askmobile', 'mobile');
$contextSwitch->setAutoDisableLayout(false);
$this->getHelper('layout')->setLayoutPath(APPLICATION_PATH . '/application/layouts/mobile');
$contextSwitch->initContext('mobile');
}
}
7.
, . , ( init() ), . , myaction, myaction.phtml myaction.mobile.phtml .
! -; -)
:
1, / . , , , .. , - , .
Basically, all you have to do is check in which context this application is and download the appropriate translation file.
Suppose you store the translation files in the \ application \ translations \ folder and you have the English and French versions of your application. In addition to the fr.php and en.php files, you must have a mobile version for each language in your translation folder: mobile_en.php and mobile_fr.php. The following code fragment will download the appropriate translation file:
Zend_Registry::set('Zend_Locale', new Zend_Locale($lang));
$langFile = APPLICATION_PATH . '/application/translations/';
if (Zend_Registry::get('context') == '\mobile') {
$langFile.= 'mobile_' . Zend_Registry::get('Zend_Locale')->getLanguage() . '.php';
} else {
$langFile.= Zend_Registry::get('Zend_Locale')->getLanguage() . '.php';
}
Zend_Registry::set(
'Zend_Translate', new Zend_Translate('array', $langFile)
);