7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
1/40
Copyright 2007, Zend Technologies Inc.
Building Rich InternetApplications with PHP and
Zend Framework (using AJAX)
IDG: RIAs offer the potential for a
fundamental shift in the experience
of Internet applications, leading toapplications that come closer to
delivering on the promise of the
Internet.
Stas Malyshev
Software Architect, Zend Technologies
Contributor, PHP Group
Bradford Cottel
Chief Evangelist, Zend Technologies
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
2/40
10-Jul-12Rich Internet Applications with PHP | Page 2
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
3/40
10-Jul-12Rich Internet Applications with PHP | Page 3
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
4/40
10-Jul-12Rich Internet Applications with PHP | Page 4
Rich Internet Applications
Goal: Deliver desktop experience in the browser
Advantages of RIAs:
Provide desktop-like feeling, including drag & drop,sliders, and UI changes without full page refreshes
More responsive Less visible interaction with the server
Asynchronous interaction with the server
Leverage the deployment advantages of the browser
Ajax is a response to the need for a richer and moreeasily deployable interface in the browser
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
5/40
10-Jul-12Rich Internet Applications with PHP | Page 5
Disadvantages of RIAs: Three tier architecture is significantly more complex
Requires in-depth knowledge of an additionallanguage and platformJavaScript & browser Few established standards
Need to deal with cross-browser compatibility
Not many tools in existence
Rich Internet Applications
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
6/40
10-Jul-12Rich Internet Applications with PHP | Page 7
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
7/4010-Jul-12Rich Internet Applications with PHP | Page 8
PHP & Web 2.0
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
8/4010-Jul-12Rich Internet Applications with PHP | Page 9
PHP == Web Integration
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
9/4010-Jul-12Rich Internet Applications with PHP | Page 10
PHP Ajax Projects
Amodules3, AJASON, AjaxAC,Cajax, HTS, jPOP, StratosFramework, PAJAX, PAJAJ, FlexibleAjax, Tiny Ajax, SimpleJax,phpwebbuilder, SAJAX, sniPEAR
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
10/4010-Jul-12Rich Internet Applications with PHP | Page 11
PHP and Ajax - Simplicity
Embedded HTML
JSON
$json = Zend_Json::encode($phpNative);
$phpNative = Zend_Json::decode($encodedValue);
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
11/4010-Jul-12Rich Internet Applications with PHP | Page 12
PHP and AJAX - Simplicity
SimpleXML
AJAX development with XML PHP 5 lets us use a simple intuitive syntax with XMLby making it act like a native object
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
12/4010-Jul-12Rich Internet Applications with PHP | Page 13
PHP for Microsoft Ajax library
Hello, World!
Name:
Response from server:
function button_click() {
HelloService.SayHello($get('name').value, function (result) {
$get('response').innerHTML = result;
});}
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
13/40Copyright 2007, Zend Technologies Inc.
DemoA ChatSample Application
Credits:Stas MalyshevPdraic BradyAndi Gutmans
Sbastien GruhierRich Window
prototypescript.aculo.us
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
14/4010-Jul-12Rich Internet Applications with PHP | Page 15
Chat demo applicationChatting
Enter Brad & Andi in two different browser sessions.
Chat back & forth between the two of us
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
15/4010-Jul-12Rich Internet Applications with PHP | Page 16
Chat demo applicationChat Log
Click Chat Log and have all your data appear inanother pane, like so:
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
16/4010-Jul-12Rich Internet Applications with PHP | Page 17
Chat demo applicationChat Log / Analyze
Click Analyze & via a Yahoo! REST interface, we analyzewords in the log and return links to the interesting ones.
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
17/40
10-Jul-12Rich Internet Applications with PHP | Page 18
Chat demo applicationChat Log / Flickr pics
Choose Flickr, click on a linked word, and Flickr returnsa set of pictures that have that word associated. Forinstance, one pic Prototype brings up is that of a Tesla.
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
18/40
10-Jul-12Rich Internet Applications with PHP | Page 19
Chat demo applicationChat Log / Flickr pics
Another Prototype picture that Flickr displays is theprototype for One Laptop Per Child.
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
19/40
10-Jul-12Rich Internet Applications with PHP | Page 20
Chat demo applicationChat Log / Amazon
Choose Amazon & AWS returns matches for the wordyou click like Lucene, for instance:
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
20/40
10-Jul-12Rich Internet Applications with PHP | Page 21
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
21/40
10-Jul-12Rich Internet Applications with PHP | Page 22
PHP 5 open-source web framework
Object-oriented, unit-testsrequired, & focused on bestpractices for modern web apps
Follows principle of extremesimplicity, making it easy to learnand easy to use for developers
Community-basedled by teamat Zend
Open-source process, hosted athttp://framework.zend.com undera business-friendly new BSD license
Corporate contributions:
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
22/40
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
23/40
10-Jul-12Rich Internet Applications with PHP | Page 24
What is the MVC component?
The heart of ZF web applications Model: domain-specific data
View: renders model (data) to UIPHP-based template engine
Controller: processes events,invokes changes in model
Simple solution for most apps Sensible defaults are built-in
Flexible and extensible
Supports advanced applications
MODELVIEW
CONTROLLER
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
24/40
10-Jul-12Rich Internet Applications with PHP | Page 25
How to use MVC: controllers
Controller classeshandle groups ofrequest URLs
http://zend.com/controller/actionThe default controller class is
IndexController
Action methods ineach controller classhandle individualrequests
http://zend.com/controller/actionThe default action method is
indexAction()
Example with default action & view:http://zend.com calls the method
indexAction() on theIndexController class obj [and w/ablank indexAction() method, theindex.phtml view is rendered].
http://zend.com/http://zend.com/7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
25/40
10-Jul-12Rich Internet Applications with PHP | Page 26
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
26/40
10-Jul-12Rich Internet Applications with PHP | Page 27
Chat Application structure
/chat
/ (index)
/log
/service
/ (index)
/name
/message
/ (index)
/log
/search
/keyword
/flickr
/amazon
IndexController
LogController
ServiceController
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
27/40
10-Jul-12Rich Internet Applications with PHP | Page 28
Controller Actions
Each controller actionmethod is responsible fordoing one specific taske.g., IndexController:
init() for setup
Index is default for UI view
Message & Name are theAJAX action callbacks
Controller binds modeland view together Message & Name dont
get rendered byZend_View_Renderer, butjust return data
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
28/40
10-Jul-12Rich Internet Applications with PHP | Page 29
MVC entry point: index.php
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
29/40
10-Jul-12Rich Internet Applications with PHP | Page 30
Model
class ChatData
Encapsulates: Implementation:
Session history SimpleXML
Search data Zend_Search_Lucene
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
30/40
10-Jul-12Rich Internet Applications with PHP | Page 31
Model: XML handling
$this->_xml = simplexml_load_file($file);
$newMessage = $this->_xml->addChild('message');
$newMessage->addChild('author', $author);$newMessage->addChild('timestamp', time());$newMessage->addChild('text', $message);
$this->_xml->asXML($this->_filename);
$newMessages = $this->_xml->xpath("/chat/message[timestamp>$last]");
Loading data
Adding new message
Saving data
Checking new messages
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
31/40
10-Jul-12Rich Internet Applications with PHP | Page 3210-Jul-12Rich Internet Applications with PHP | Page 32
Model: Search handling
$index = Zend_Search_Lucene::open($indexfile);
$messages = $this->getNewMessages($since);
foreach($messages as $newmsg) {$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('timestamp',$newmsg['timestamp']));$doc->addField(Zend_Search_Lucene_Field::Text('author',
$newmsg['author']));$doc->addField(Zend_Search_Lucene_Field::Text('text',
$newmsg['text']));$index->addDocument($doc);
}
Indexing
Searching$index = Zend_Search_Lucene::open($indexfile);$hits = $index->find($query);return $hits;
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
32/40
10-Jul-12Rich Internet Applications with PHP | Page 33
View
Each action has its own viewtemplate
Templates rendered
automatically Unless requested not to, i.e.
$this->_helper->viewRenderer->setNoRender(true);
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
33/40
10-Jul-12Rich Internet Applications with PHP | Page 34
View: displaying data
Looking for '':
[]:
Nothing found for '', sorry.
Display search results
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
34/40
10-Jul-12Rich Internet Applications with PHP | Page 3510-Jul-12Rich Internet Applications with PHP | Page 35
AJAX communication - JSON
// This function returns JSON (not a template), so dontrender a template$this->_helper->viewRenderer->setNoRender(true);
$phpMessageArray = $data->getNewMessages($last);$onlineUsersArray = $data->getOnlineUsers();
$jsonArray = array( 'newmessages'=>$phpMessageArray,'onlineusers'=>$onlineUsersArray );
$responseJSON = Zend_Json::encode($jsonArray);
$this->getResponse()->setHeader('Content-Type', 'text/plain');$this->getResponse()->setBody($responseJSON);
PHP data to JSON
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
35/40
10-Jul-12Rich Internet Applications with PHP | Page 3610-Jul-12Rich Internet Applications with PHP | Page 36
AJAX communication - JSON
$uri = 'http://search.yahooapis.com';$service = '/ContentAnalysisService/V1/termExtraction';$request = array( 'appid' =>$this->yahoo_key,
'context' =>$text,'output' =>'xml' );
$rest = new Zend_Rest_Client();$rest->setURI($uri);
$response = $rest->restPost($service, $request);
$this->getResponse()->setHeader('Content-Type', 'text/plain');$this->getResponse()->setBody(
Zend_Json::fromXML($response->getBody()));
XML to JSON
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
36/40
10-Jul-12Rich Internet Applications with PHP | Page 37
Handling services - Flickr
$flickr = new Zend_Service_Flickr($this->flickr_key);
$flickrSearchptions = array('page'=>1,'sort'=>'interestingness-desc');$results = $flickr->tagSearch($keywords,$flickrSearchptions);// Collect results into PHP array$phpRes = array();foreach($results as $result) {$newres = array();
$newres['id'] = $result->id;$newres['title'] = $result->title;$img = $result->Small;$newres['src'] = $img->uri;$newres['w'] = $img->width;$newres['h'] = $img->height;
$newres['clickUri'] = @$result->Original->clickUri;$phpRes[] = $newres;}// Send the results out as JSON data$this->getResponse()->setHeader('Content-Type', 'text/plain');$this->getResponse()->setBody(Zend_Json::encode($phpRes));
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
37/40
10-Jul-12Rich Internet Applications with PHP | Page 38
Handling servicesYahoo! through REST API
$uri = 'http://search.yahooapis.com';$service = '/ContentAnalysisService/V1/termExtraction';$request = array(
'appid' =>$this->yahoo_key,'context' =>$text,'output' =>'xml'
);
$rest = new Zend_Rest_Client();$rest->setURI($uri);$response = $rest->restPost($service, $request);
// Send response converted to JSON$this->getResponse()->setHeader('Content-Type', 'text/plain');
$this->getResponse()->setBody(Zend_Json::fromXML($response->getBody()));
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
38/40
10-Jul-12Rich Internet Applications with PHP | Page 39
OverviewBuilding RIAs with PHP & Zend
RIAs: Advantages & Disadvantages
Why AJAX with PHP?
Demo
Why AJAX with PHP on Zend Framework?
Code walkthrough
Whats next for RIAs with PHP on Zend?
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
39/40
10-Jul-12Rich Internet Applications with PHP | Page 4010-Jul-12Rich Internet Applications with PHP | Page 40
Whats next?
AJAX-enabled Form component in Zend Framework
Working on requirements and proposal
AJAX support in development toolsEclipse-based JavaScript editingsyntax highlighting, code completion
JavaScript debugging
Toolkit support (for instance, class browsers)
Opens up opportunity for using Flex
Significantly grow support for Web Services vendors
Important enhancements to our Lucene implementation Range queries, wildcard queries
Support for Lucene 2.3 file format (faster, better, backwards compatible, )
Zend Component Model Server - PHP component architecture
Client - Ajax Toolkit, Client side messaging, Client-Server connectivity
Development tools - Tooling for components
7/31/2019 RIAs With PHP and Zend Framework Using Ajax Webinar
40/40
Thanks!
stas AT zend DOT comStas Malyshev
brad AT zend DOT comBradford Cottel
mailto:[email protected]:[email protected]:[email protected]:[email protected]