30
Copyright © 2007, Zend Technologies Inc. Rich Internet Applications with PHP IDC: RIA 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. PHP delivers modern Web applications Stanislav Malyshev Zend Technologies

Rich Internet Applications with PHP

  • Upload
    alifr

  • View
    118

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Rich Internet Applications with PHP

Copyright © 2007, Zend Technologies Inc.

Rich Internet Applications with PHP

IDC: RIA 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.

PHP delivers modern Web applications

Stanislav MalyshevZend Technologies

Page 2: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 2

Rich Internet Applications

• Goal: Delivering the desktop experience in the browser

• Advantages of RIAs: Richer user interfaces

• 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 easily

deployable interface in the browser

Page 3: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 3

Disadvantages of RIA

• Three tier architecture is significantly more complex

• Requires in-depth knowledge of an additional language and platform, Javascript & the browser

• Few established standards• Need to deal with cross-browser compatibility• Few tools in existence

Page 4: Rich Internet Applications with PHP

Apr 8, 2023Making Zend Rich|

Page 4

John F. Andrews, President, Evans Data Corporation, 2007:

Close to half of developers surveyed say they are already working with AJAX or plan to do so in the coming year.

Consistent with the increasing adoption of web services we are also seeing the same for AJAX. This framework, now more than ever, is allowing developers the means to make web-based applications function more like desktop ones.

Ajax Momentum

Gartner, 2006:

Ajax is also rated as high impact and capable of reaching maturity in less than two years. …  High levels of impact and business value can only be achieved when the development process encompasses innovations in usability and reliance on complementary server-side processing (as is done in Google Maps). ”

“ ”“

Page 5: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 5

PHP & Web 2.0

Page 6: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 6

PHP == Web Integration

Page 7: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 7

PHP Ajax Projects

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

Page 8: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 8

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 9: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 9

PHP and Ajax - Simplicity

SimpleXML

<?php

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

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

• PHP enables overloading of OO syntax both from extensions and user-land classes

• Enables exposing OO-like models natively (XML, SOAP, Java, …)

Page 10: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 10

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 11: Rich Internet Applications with PHP

Copyright © 2007, Zend Technologies Inc.

Sample Application

Credits:Stas MalyshevPádraic Brady

Sébastien Gruhier – Rich Windowprototype

script.aculo.us

Page 12: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 12

• High-quality PHP 5 open-source framework

• Easy-to-use, powerful functionality, focusing on the best practices of robust, secure and modern Web applications.

• Follows principle of “extreme simplicity,” which makes it easy to learn and easy to use for programmers

• Developed by members of the PHP community, led by a team at Zend

• Open-source process, hosted at framework.zend.com under the business-friendly BSD license

• Corporate contributions:

Page 13: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 13

Zend Framework Architecture

Page 14: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 14

What is the MVC component?

• The heart of ZF web applications Model: domain-specific

representation of data View: renders model data into

a user-interface element; PHP-based template engine

Controller: processes events, invokes changes in models

• Simple solution in most applications Sensible defaults are built in Flexible and extensible Supports advanced applications

MODELVIEW

CONTROLLER

Page 15: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 15

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()”

Page 16: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 16

Chat Application

/chat

/ (index)

/log

/service

/ (index)

/name

/message

/ (index)

/log

/search

/keyword

/flickr

/amazon

IndexController

LogController

ServiceController

Page 17: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 17

Controller Actions

• Each controller action method is responsible for doing one specific task

• Controller binds model and view together

Page 18: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 18

MVC entry point: index.php

<?php$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);// Go!$controller->dispatch();

Page 19: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 19

Model

Class ChatDataEncapsulates XML storage

Implemented as SimpleXMLEncapsulates data search

Implemented as Zend_Search_Lucene

Page 20: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 20

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 21: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 21

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

Page 22: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 22

Model: Search handling

Searching

$index = Zend_Search_Lucene::open($indexfile);$hits = $index->find($query);return $hits;

Page 23: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 23

View

• Each action has its own view template

• Templates rendered automatically Unless the user asks not to

Page 24: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 24

View – displaying search results

<? if(count($this->hits)): $day = ""; ?>

<span class="searchterm">Looking for '<? echo $this->query; ?>':</span><br><? foreach($this->hits as $message) {

$mday = date("d-M-y", $message->timestamp);if($day != $mday) {

$day = $mday;echo "<p class=\"day\">$day</p>"; } ?>

<p><span class="time"><? echo date('H:i:s', $message->timestamp) ?></span> <span class="screenname"><? echo $message->author; ?></span>: <span class="msgtext"><? echo $message->text; ?></span> </p><? } else: ?>Nothing found for '<? echo $this->query; ?>', sorry.<? endif; ?>

Page 25: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 25

AJAX communication - JSON

// This function returns JSON, not 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 26: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 26

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 27: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 27

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 28: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 28

What’s next?

• Ajax-enabled Form component in Zend Framework 1.1 ~Oct 2007

• Ajax support in development tools – Eclipse-based Javascript editing – syntax highlighting, code completion Javascript debugging Toolkit support (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, …)

Page 29: Rich Internet Applications with PHP

Apr 8, 2023Rich Internet Applications with PHP|

Page 29

What’s next?

• Zend Component Model Server:

• PHP component architecture Client:

• Ajax Toolkit• Client side messaging• Client-server connectivity

Development tools:• Tooling for components

Page 30: Rich Internet Applications with PHP

Copyright © 2007, Zend Technologies Inc.

Thanks!

Stanislav Malyshev [email protected]://devzone.zend.com/

http://framework.zend.com/