click here to download the webinar slides

40
Copyright © 2007, Zend Technologies Inc. Building Rich Internet Applications with PHP and Zend Framework (using AJAX) IDG: RIAs offer the potential for a fundamental shift in the experience of Internet applications, leading to applications 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

Upload: sampetruda

Post on 17-May-2015

821 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Click here to download the Webinar Slides

Copyright © 2007, Zend Technologies Inc.

Building Rich Internet Applications with PHP and Zend Framework (using AJAX)

IDG: RIAs offer the potential for a fundamental shift in the experience of Internet applications, leading to applications that come closer to delivering on the promise of the Internet.

Stas MalyshevSoftware Architect, Zend TechnologiesContributor, PHP Group

Bradford CottelChief Evangelist, Zend Technologies

Page 2: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 2

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 3: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 3

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 4: Click here to download the Webinar Slides

Apr 12, 2023Rich 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 more easily deployable interface in the browser

Page 5: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 5

• Disadvantages of RIAs:

Three tier architecture is significantly more complex

• Requires in-depth knowledge of an additional language and platform—JavaScript & browser Few established standards Need to deal with cross-browser compatibility

• Not many tools in existence

Rich Internet Applications

Page 6: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 7

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 7: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 8

PHP & Web 2.0

Page 8: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 9

PHP == Web Integration

Page 9: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 10

PHP Ajax Projects

Amodules3, AJASON, AjaxAC, Cajax, HTS, jPOP, Stratos Framework, PAJAX, PAJAJ, Flexible Ajax, Tiny Ajax, SimpleJax, phpwebbuilder, SAJAX, sniPEAR

Page 10: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 11

PHP and Ajax - Simplicity

Embedded HTML<div id="DataTable"><?php $page->DataTable->show(); ?></div>

JSON

$json = Zend_Json::encode($phpNative);

$phpNative = Zend_Json::decode($encodedValue);

Page 11: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 12

PHP and AJAX - Simplicity

SimpleXML

<?php

$clients = simplexml_load_file('clients.xml');foreach($clients as $client) {

print "{$client->name} is {$client->desc}\n";}?>

• AJAX development with XML

• PHP 5 lets us use a simple intuitive syntax with XML by making it act like a native object

Page 12: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 13

PHP for Microsoft Ajax library

<html> <head> <title>Hello, World!</title> <script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script> <script type="text/javascript" src="HelloService.php/js"></script> </head><body>Name: <input id="name" type="text" /><input type="button" value="Say Hello" onclick="button_click(); return false;" /><br />Response from server: <span id="response"></span></body><script type="text/javascript"> function button_click() { HelloService.SayHello($get('name').value, function (result) { $get('response').innerHTML = result; }); }</script></html>

<?php require_once '../../dist/MSAjaxService.php'; class HelloService extends MSAjaxService{ function SayHello($name) { return "Hello, " . $name . "!"; }} $h = new HelloService();$h->ProcessRequest(); ?>

Page 13: Click here to download the Webinar Slides

Copyright © 2007, Zend Technologies Inc.

Demo – A ChatSample Application

Credits:Stas MalyshevPádraic BradyAndi Gutmans

Sébastien Gruhier – Rich Windowprototype

script.aculo.us

Page 14: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 15

Chat demo application – Chatting

• Enter ‘Brad’ & ‘Andi’ in two different browser sessions.

• Chat back & forth between the two of us…

Page 15: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 16

Chat demo application – Chat Log

• Click Chat Log and have all your data appear in another pane, like so:

Page 16: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 17

Chat demo application – Chat Log / Analyze

• Click Analyze & via a Yahoo! REST interface, we analyze words in the log and return links to the ‘interesting’ ones.

Page 17: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 18

Chat demo application – Chat Log / Flickr pics

• Choose Flickr, click on a linked word, and Flickr returns a set of pictures that have that word associated. For instance, one pic Prototype brings up is that of a Tesla.

Page 18: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 19

Chat demo application – Chat Log / Flickr pics

• Another Prototype picture that Flickr displays is the prototype for One Laptop Per Child.

Page 19: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 20

Chat demo application – Chat Log / Amazon

• Choose Amazon & AWS returns matches for the word you click… like Lucene, for instance:

Page 20: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 21

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 21: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 22

• PHP 5 open-source web framework

• Object-oriented, unit-tests required, & focused on best practices for modern web apps

• Follows principle of “extreme simplicity,” making it easy to learn and easy to use for developers

• Community-based—led by team at Zend

• Open-source process, hosted at http://framework.zend.com under a business-friendly new BSD license

• Corporate contributions:

Page 22: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 23

Zend Framework Architecture

Page 23: Click here to download the Webinar Slides

Apr 12, 2023Rich 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 UI

PHP-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

Page 24: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 25

How to use MVC: controllers

• Controller classes handle groups of request URLs

http://zend.com/controller/actionThe default controller class is

“IndexController”

• Action methods in each controller class handle individual requests

http://zend.com/controller/actionThe default action method is

“indexAction()”

Example with default action & view:http://zend.com calls the method

indexAction() on the IndexController class obj [and w/a blank indexAction() method, the index.phtml view is rendered].

Page 25: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 26

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 26: Click here to download the Webinar Slides

Apr 12, 2023Rich 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

Page 27: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 28

Controller Actions

• Each controller action method is responsible for doing one specific taske.g., IndexController: init() for setup Index is default for UI view Message & Name are the

AJAX action callbacks

• Controller binds model and view together Message & Name don’t

get rendered by Zend_View_Renderer, but just return data

Page 28: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 29

MVC entry point: index.php

<?php// Get previous session data from .xml file$config = new Zend_Config(array(), true);$config->datafile = './data/chat.xml';

// Store the config for other parts to useZend_Registry::set('config', $config);

// Setup and run the Front Controller$controller = Zend_Controller_Front::getInstance();$controller->setControllerDirectory('./application/controllers');$controller->throwExceptions(true); // Exceptions ON for dev mode

// Go!$controller->dispatch();?>

Page 29: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 30

Model

class ChatData Encapsulates: Implementation: Session history SimpleXML Search data Zend_Search_Lucene

Page 30: Click here to download the Webinar Slides

Apr 12, 2023Rich 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

Page 31: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 32

Apr 12, 2023Rich 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;

Page 32: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 33

View

• Each action has its own view template

• Templates rendered automatically Unless requested not to, i.e.

$this->_helper->viewRenderer->setNoRender(true);

Page 33: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 34

View: displaying data

<? if(count($this->hits)) { ?>

<span class="searchterm">Looking for '<? echo $this->query; ?>':</span><br/>

<? foreach($this->hits as $message) { ?> [<? echo date('c', $message->timestamp) ?>] <span class="screenname"><? echo $message->author; ?></span>: <span class="msgtext"><? echo $message->text; ?></span> <br/>

<? } // if(count($this->hits)) } else { ?>Nothing found for '<? echo $this->query; ?>', sorry.<? } ?>

Display search results

Page 34: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 35

Apr 12, 2023Rich Internet Applications with PHP | Page 35

AJAX communication - JSON

// This function returns JSON (not a template), so don’t render 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

Page 35: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 36

Apr 12, 2023Rich 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

Page 36: Click here to download the Webinar Slides

Apr 12, 2023Rich 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));

Page 37: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 38

Handling services – Yahoo! 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()));

Page 38: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 39

Overview – Building RIAs with PHP & Zend

• RIAs: Advantages & Disadvantages

• Why AJAX with PHP?

• Demo

• Why AJAX with PHP on Zend Framework?

• Code walkthrough

• What’s next for RIAs with PHP on Zend?

Page 39: Click here to download the Webinar Slides

Apr 12, 2023Rich Internet Applications with PHP|

Page 40

Apr 12, 2023Rich Internet Applications with PHP | Page 40

What’s next?

• AJAX-enabled Form component in Zend Framework Working on requirements and proposal

• AJAX support in development tools – Eclipse-based JavaScript editing – syntax 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

Page 40: Click here to download the Webinar Slides

Copyright © 2007, Zend Technologies Inc.

Thanks!

Stas Malyshev stas AT zend DOT com

Bradford Cottel brad AT zend DOT com