© All rights reserved. Zend Technologies, Inc. Sviluppo di applicazioni web mobile con Zend Framework Enrico Zimuel Senior PHP Engineer, Zend Technologies Zend Framework Core Team PHP goes mobile, 13 Aprile 2012 http://mobilephp.grusp.org/
Aug 23, 2014
© All rights reserved. Zend Technologies, Inc.
Sviluppo di applicazioni webmobile con Zend Framework
Enrico Zimuel Senior PHP Engineer, Zend TechnologiesZend Framework Core Team
PHP goes mobile, 13 Aprile 2012http://mobilephp.grusp.org/
© All rights reserved. Zend Technologies, Inc.
Chi sono
● Software Engineer dal 1996 (1984?)
▶ Assembly, Basic, C/C++, Java, Perl, PHP
● PHP Engineer e Software Architect presso Zend Technologies dal 2008
● Zend Framework Core Team dal 2011
● Research Programmer presso l'Informatics Institute dell'Università di Amsterdam
● Co-autore del libro “PHP best practices” FAG edizioni (in corso di pubblicazione)
© All rights reserved. Zend Technologies, Inc.
Web mobile vs. Native App
© All rights reserved. Zend Technologies, Inc.
Web mobile vs. Native App
Web mobile Native App
Accesso al dispositivo
limitato totale
Velocità buona ottima
Costi sviluppo bassi medio/alti
Tecnologia open si a volte
Ricavi 100% dipende
Approvazione nessuna dipende
Tempi di rilascio immediati 1-2 settimane
Linguaggio di programmazione
HTML e qualsiasi altro (esempio PHP)
dipende
© All rights reserved. Zend Technologies, Inc.
“People never cared about the Web vs. apps and devices . . . They want free stuff, entertainment, and services when they want them, and on the device they have in front of them”
Web mobile vs. Native App (2)
Fonte: Pew Internet Project (Marzo, 2012)
© All rights reserved. Zend Technologies, Inc.
Web mobile con ZF
© All rights reserved. Zend Technologies, Inc.
Zend Framework
● Framework PHP per lo sviluppo di applicazioni web
▶ > 10 milioni di download▶ > 500 sviluppatori▶ > 10 milioni hits su Google
● Open source, licenza basata su new BSD
● Versioni: 1.11.11 (PHP 5.2) e 2.0.0.beta3 (PHP 5.3+)
● Sito del progetto: http://framework.zend.com
● © Zend Technologies Ltd.
© All rights reserved. Zend Technologies, Inc.
Zend Technologies
● Zend Technologies Ltd, the PHP Company
● Fondata nel 1999 da Andi Gutmans e Zeev Suraski, gli sviluppatori del PHP 3.0
● Quartier generale nella Silicon Valley a Cupertino (USA)
● Uffici in: Francia, Germania, India, Irlanda, Israele, Italia
● © PHP Engine 2 (l'interprete del PHP)
● Sito: http://www.zend.com
© All rights reserved. Zend Technologies, Inc.
Componenti ZF per il web mobile
● Zend_Http_UserAgent▶ BrowsCap▶ Tera-WURFL▶ DeviceAtlas
● Context Switching● Zend_Mobile
▶ Disponibile dalla versione 1.12 di ZF
© All rights reserved. Zend Technologies, Inc.
BrowsCap
● Funzione get_browser() del PHP
● Configurazione:
▶ Download del file browscap.ini dal sito del progetto Browser Capabilities: http://browsers.garykeith.com/downloads
▶ Configurare php.ini con la direttiva: browscap=/path/to/browscap.ini
© All rights reserved. Zend Technologies, Inc.
Tera-WURFL
● Tera-WURFL è una libreria in PHP e un database in MySQL/MSSQL/MongoDB di informazioni su device
● Download: http://dbapi.scientiamobile.com
© All rights reserved. Zend Technologies, Inc.
DeviceAtlas
● Libreria disponibile per diversi linguaggi (PHP, Java, .NET, Python, Ruby) per il riconoscimento di mobile device
● Download: http://deviceatlas.com/resourcecentre/get+started/enterprise+api
© All rights reserved. Zend Technologies, Inc.
Zend_Http_UserAgent
● Utilizzato per identificare il device dell'utente● Utilizzabile in diversi punti di un'applicazione ZF
▶ Plugin (bootstrap)▶ Controller▶ View
© All rights reserved. Zend Technologies, Inc.
Zend_Http_UserAgent (2)
● Plugin (bootstrap)
▶
● Controller
▶
● View
▶
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');$userAgent = $bootstrap->getResource('useragent');
$bootstrap = $this->getInvokeArg('bootstrap');$userAgent = $bootstrap->getResource('useragent');
$userAgent = $this->userAgent();
© All rights reserved. Zend Technologies, Inc.
Alcuni metodi dell'UserAgent
● getDevice()
● getBrowserType()
● getAllFeatures()
● hasFlashSupport()
● hasPdfSupport()
● hasPhoneNumber()
● httpsSupport()
● getMaxImageHeight()
● getMaxImageWidth()
● ...
© All rights reserved. Zend Technologies, Inc.
Context switching
● ContextSwitching è un action helper che consente di definire risposte differenti a seconda del contesto (della richiesta)
● E' di solito utilizzato nei web services per differenziare la risposta (ad esempio Json o XML)
● Permette di definire contesti custom
▶ Aggiungere dei suffissi alle viste▶ Modificare l'header HTTP▶ Definire delle callback
© All rights reserved. Zend Technologies, Inc.
Esempio
class Application_Plugin_Mobile extends Zend_Controller_Plugin_Abstract{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{$contextSwitch =
Zend_Controller_Action_HelperBroker::getStaticHelper('ContextSwitch'); $contextSwitch->clearContexts()
->setContext('iphone', array( 'suffix' => 'iphone', 'headers' => array(
'Content-Type' => 'text/html;Charset=UTF-8'), )) ->setContext('html', array( 'suffix' => 'html', 'headers' => array(
'Content-Type' => 'text/html;Charset=UTF-8'), )) ->setAutoDisableLayout(false) ->setDefaultcontext('html') ->initContext();
© All rights reserved. Zend Technologies, Inc.
Esempio (2)
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');$userAgent = $bootstrap->getResource('useragent');switch($userAgent->getDevice()->getFeature('device')){
case 'iphone':$request->setParam('format','iphone');break;
default:$request->setParam('format','html');
}}
}
© All rights reserved. Zend Technologies, Inc.
Esempio (3)
class IndexController extends Zend_Controller_Action{
public function init(){
$this->_helper->contextSwitch() ->addActionContext('index', 'iphone') ->initContext();
}
…}
View
index.phtmlindex.iphone.phtml
© All rights reserved. Zend Technologies, Inc.
Zend_Mobile
● Zend_Mobile_Push▶ Invio di notifiche push verso specifici vendor
● APNS (iTouch/iPad/iPhone)● C2DM (Google Android)● MPNS (Windows Phone)
© All rights reserved. Zend Technologies, Inc.
Esempio
$message = new Zend_Mobile_Push_Message_Apns();$message->setAlert('Zend Mobile Push Example');$message->setBadge(1);$message->setSound('default');$message->setId(time());$message->setToken('ABCDEF0123456789');
$apns = new Zend_Mobile_Push_Apns();$apns->setCertificate('/path/to/provisioning-certificate.pem');// if you have a passphrase on your certificate:// $apns->setCertificatePassphrase('foobar');...
© All rights reserved. Zend Technologies, Inc.
Esempio (2)
try {$apns->connect(Zend_Mobile_Push_Apns::SERVER_SANDBOX_URI);
} catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {// you can either attempt to reconnect here or try again later
exit(1);} catch (Zend_Mobile_Push_Exception $e) {
echo 'APNS Connection Error:' . $e->getMessage();exit(1);
}
try {$apns->send($message);
} catch (Zend_Mobile_Push_Exception_InvalidToken $e) {echo $e->getMessage();
} catch (Zend_Mobile_Push_Exception $e) {echo $e->getMessage();
}
$apns->close();
© All rights reserved. Zend Technologies, Inc.
Domande?
© All rights reserved. Zend Technologies, Inc.
Grazie!
Per maggiori informazioni:http://framework.zend.com/