Rich Internet Applications With PHP
Rich Internet Applications With PHP
Stanislav Malyshev
Zend Technologies
• 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
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Disadvantages of RIA
Gartner, 2006:
”
complementary server-side processing (as is done in Google
Maps).
“
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.
|
Making Zend Rich Mar 10, 2008 Page
PHP & Web 2.0
|
Rich Internet Applications with PHP Mar 10, 2008 Page
PHP == Web Integration
|
Rich Internet Applications with PHP Mar 10, 2008 Page
PHP Ajax Projects
|
Rich Internet Applications with PHP Mar 10, 2008 Page
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);
|
Rich Internet Applications with PHP Mar 10, 2008 Page
PHP and Ajax - Simplicity
SimpleXML
<?php
$clients = simplexml_load_file('clients.xml');
foreach($clients as $client) {
print "{$client->name} is {$client->desc}\n";
}
?>
|
Rich Internet Applications with PHP Mar 10, 2008 Page
PHP for Microsoft Ajax library
<?php
require_once '../../dist/MSAjaxService.php';
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Sample Application
Credits:
Stas Malyshev
Pádraic Brady
|
Rich Internet Applications with PHP Mar 10, 2008 Page
What is the MVC component?
|
Rich Internet Applications with PHP Mar 10, 2008 Page
How to use MVC: controllers
• Controller classes
handle groups of
request URLs
http://zend.com/controller/action
The default controller class is
“IndexController”
• Action methods in
each controller
class handle
individual
requests
http://zend.com/controller/action
The default action method is
“indexAction()”
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Chat Application
IndexController / (index)
/ (index) /name
/message
LogController / (index)
/log /log
/chat
/search
ServiceController /keyword
/flickr
/service
/amazon
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Controller Actions
|
Rich Internet Applications with PHP Mar 10, 2008 Page
MVC entry point: index.php
<?php
$config = new Zend_Config(array(), true);
$config->datafile = './data/chat.xml';
// Store the config for other parts to use
Zend_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();
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Model
Class ChatData
Encapsulates XML storage
Implemented as SimpleXML
Encapsulates data search
Implemented as Zend_Search_Lucene
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Model: XML handling
Loading data
$this->_xml = simplexml_load_file($file);
$newMessage = $this->_xml->addChild('message');
$newMessage->addChild('author', $author);
$newMessage->addChild('timestamp', time());
$newMessage->addChild('text', $message);
Saving data
$this->_xml->asXML($this->_filename);
Indexing
$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);
}
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Model: Search handling
Searching
$index = Zend_Search_Lucene::open($indexfile);
$hits = $index->find($query);
return $hits;
|
Rich Internet Applications with PHP Mar 10, 2008 Page
View
• Templates rendered
automatically
Unless the user asks not to
|
Rich Internet Applications with PHP Mar 10, 2008 Page
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; ?>
|
Rich Internet Applications with PHP Mar 10, 2008 Page
AJAX communication - JSON
|
Rich Internet Applications with PHP Mar 10, 2008 Page
AJAX communication - JSON
XML to 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()));
|
Rich Internet Applications with PHP Mar 10, 2008 Page
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));
|
Rich Internet Applications with PHP Mar 10, 2008 Page
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, …)
|
Rich Internet Applications with PHP Mar 10, 2008 Page
What’s next?
|
Rich Internet Applications with PHP Mar 10, 2008 Page
Thanks!
Stanislav Malyshev [email protected]
http://devzone.zend.com/
http://framework.zend.com/