Building Rich Internet Applications with PHP and Zend
Building Rich Internet Applications with PHP and Zend Framework Stanislav Malyshev Software Architect, Zend Technologies 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. Copyright © 2007, Zend Technologies Inc.
Overview – Building RIAs with PHP & Zend • RIAs: Advantages & Disadvantages • Demo • Why AJAX with PHP on Zend Framework? • Code walkthrough • What’s next for RIAs with PHP on Zend? • Future Demo Rich Internet Applications with PHP 6/16/2021 2
Rich Internet Applications • Goal: Deliver desktop experience in the browser • Advantages of RIAs: § Ajax is a response to the need for a richer and more easily deployable interface in the browser § 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 Rich Internet Applications with PHP 6/16/2021 3
Rich Internet Applications • Disadvantages of RIAs: § Three tier architecture is significantly more complex • Requires in-depth knowledge of an additional language and platform—Java. Script & browser § Few established standards § Need to deal with cross-browser compatibility • Not many tools in existence Rich Internet Applications with PHP 6/16/2021 4
Demo – A Chat Sample Application Credits: Stas Malyshev Pádraic Brady Sébastien Gruhier – Rich Window prototype script. aculo. us Copyright © 2007, Zend Technologies Inc.
Code Walkthough • Framework • MVC • Data handling • Search • Services communication Rich Internet Applications with PHP 6/16/2021 6
• 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: Rich Internet Applications with PHP 6/16/2021 7
Zend Framework Architecture Rich Internet Applications with PHP 6/16/2021 8
What is the MVC component? • The heart of ZF web applications § Model: domain-specific data § View: renders data to UI – PHP-based template engine § Controller: processes events, invokes changes in model CONTROLLER VIEW MODEL • Simple solution for most apps § Sensible defaults are built-in § Flexible and extensible § Supports advanced applications Rich Internet Applications with PHP 6/16/2021 9
How to use MVC: controllers • Controller classes handle groups of request URLs http: //zend. com/controller/action The default controller class is “Index. Controller” • Action methods in each controller class handle individual requests http: //zend. com/controller/action The default action method is “index. Action()” Rich Internet Applications with PHP 6/16/2021 10
Chat Application structure Index. Controller / (index) /name /message Log. Controller /chat /log / (index) /log /search Service. Controller /service /keyword /flickr /amazon Rich Internet Applications with PHP 6/16/2021 11
Controller Actions • Each controller action method is responsible for doing one specific task e. g. , Index. Controller: § 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 Rich Internet Applications with PHP 6/16/2021 12
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 use Zend_Registry: : set('config', $config); // Setup and run the Front Controller $controller = Zend_Controller_Front: : get. Instance(); $controller->set. Controller. Directory('. /application/controllers'); $controller->throw. Exceptions(true); // Exceptions ON for dev mode // Go! $controller->dispatch(); ? > Rich Internet Applications with PHP 6/16/2021 13
Model class Chat. Data Encapsulates: Session history Search data Rich Internet Applications with PHP Implementation: Simple. XML Zend_Search_Lucene 6/16/2021 14
Model: XML handling Loading data $this->_xml = simplexml_load_file($file); Adding new message $new. Message = $this->_xml->add. Child('message'); $new. Message->add. Child('author', $author); $new. Message->add. Child('timestamp', time()); $new. Message->add. Child('text', $message); Saving data $this->_xml->as. XML($this->_filename); Checking new messages $new. Messages = $this->_xml->xpath("/chat/message[timestamp>$last]"); Rich Internet Applications with PHP 6/16/2021 15
Model: Search handling Indexing $index = Zend_Search_Lucene: : open($indexfile); $messages = $this->get. New. Messages($since); foreach($messages as $newmsg) { $doc = new Zend_Search_Lucene_Document(); $doc->add. Field(Zend_Search_Lucene_Field: : Un. Indexed('timestamp', $newmsg['timestamp'])); $doc->add. Field(Zend_Search_Lucene_Field: : Text('author', $newmsg['author'])); $doc->add. Field(Zend_Search_Lucene_Field: : Text('text', $newmsg['text'])); $index->add. Document($doc); } Searching $index = Zend_Search_Lucene: : open($indexfile); $hits = $index->find($query); return $hits; Rich Internet Applications with PHP 16 -Jun-21 6/16/2021 | Page 16
View • Each action has its own view template • Templates rendered automatically § Unless requested not to, i. e. $this->_helper->view. Renderer-> set. No. Render(true); Rich Internet Applications with PHP 6/16/2021 17
View: displaying data Display search results <? if(count($this->hits)) { ? > <span class="searchterm">Looking for '<? = $this->query; ? >': </span><br/> <? foreach($this->hits as $message) { ? > [<? = date('c', $message->timestamp) ? >] <span class="screenname"><? = $message->author; ? ></span>: <span class="msgtext"><? = $message->text; ? ></span> <br/> <? } // if(count($this->hits)) } else { ? > Nothing found for '<? = $this->query; ? >', sorry. <? } ? > Rich Internet Applications with PHP 6/16/2021 18
AJAX communication - JSON PHP data to JSON // This function returns JSON (not a template), // so don’t render a template $this->_helper->view. Renderer->set. No. Render(true); $php. Message. Array = $data->get. New. Messages($last); $online. Users. Array = $data->get. Online. Users(); $json. Array = array('newmessages'=>$php. Message. Array, 'onlineusers'=>$online. Users. Array); $response. JSON = Zend_Json: : encode($json. Array); $this->get. Response()->set. Header('Content-Type', 'text/plain'); $this->get. Response()->set. Body($response. JSON); Rich Internet Applications with PHP 16 -Jun-21 6/16/2021 | Page 19
AJAX communication - JSON XML to JSON $uri = 'http: //search. yahooapis. com'; $service = '/Content. Analysis. Service/V 1/term. Extraction'; $request = array('appid‘ => $this->yahoo_key, 'context' => $text, 'output' => 'xml' ); $rest = new Zend_Rest_Client(); $rest->set. URI($uri); $response = $rest->rest. Post($service, $request); $this->get. Response()->set. Header('Content-Type', 'text/plain'); $this->get. Response()->set. Body( Zend_Json: : from. XML($response->get. Body())); Rich Internet Applications with PHP 16 -Jun-21 6/16/2021 | Page 20
Handling services - Flickr $flickr = new Zend_Service_Flickr($this->flickr_key); $flickr. Searchptions = array('page'=>1, 'sort'=>'interestingness-desc'); $results = $flickr->tag. Search($keywords, $flickr. Searchptions); // Collect results into PHP array $php. Res = 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['click. Uri'] = @$result->Original->click. Uri; $php. Res[] = $newres; } // Send the results out as JSON data $this->get. Response()->set. Header('Content-Type', 'text/plain'); $this->get. Response()->set. Body(Zend_Json: : encode($php. Res)); Rich Internet Applications with PHP 6/16/2021 21
What’s next? • AJAX-enabled Form component in Zend Framework § Working on requirements and proposal • AJAX support in development tools – Eclipse-based § § Java. Script editing – syntax highlighting, code completion Java. Script 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, …) Rich Internet Applications with PHP 16 -Jun-21 6/16/2021 | Page 22
Zend Component Model What? Toolkit for building AJAX applications How? Building blocks model – a-la VB/Delphi/ASP/JSF Why? RAD in PHP spirit – simplicity + power Rich Internet Applications with PHP 6/16/2021 23
Zend Component Model • Zend Framework § Component model • PHP component architecture • Server side messaging & persistence • Ajax controller § Ajax Toolkit • Client side messaging • Client-server connectivity • Zend Studio (Eclipse) § § § Drag&drop of components Component configuration Ajax Toolkit support (class browsing, documentation, etc. ) Javascript editing and debugging Advanced CSS support Cross language debugging (stepping from Javascript into PHP) Rich Internet Applications with PHP 6/16/2021 24
Zend Component Model • Component architecture § Both visual and non-visual components • Rich-design time experience ala Delphi and Visual Basic § Drag&drop of components onto the Web page • Component is self-contained incl. design and runtime rendering & property editors § IDEs don’t have to be hard coded per-component § Encourages eco-system of components • WYSIAWYG • Programmatic model § PHP developers like code and not declarative syntax Rich Internet Applications with PHP 6/16/2021 25
What are components? • PHP+JS+CSS client/server side bundle • Self-describing components – can be used with • any tool Visual RAD § design time view – no data required § production view - reflects actual data • Vi-compatibility – no additional configurations or • declarative syntax files, only PHP and CSS/JS on client side Extensibility – new components, extending existing components Rich Internet Applications with PHP 6/16/2021 26
Components Demo Rich Internet Applications with PHP 6/16/2021 27
Components Demo Table component Button component Rich Internet Applications with PHP GMap component Frame component containing Table component 6/16/2021 28
Demo – Server code – setup() <? php class My_Page extends Zend_Component_Page { function setup() { $this->DB 1 = $this->create('DB_Table', 'DB 1'); $this->DB 1 ->driver = 'PDO_MYSQL'; $this->Contacts. Table = $this->create('AJAX_Table_Paged', 'Contacts. Table'); $this->Contacts. Table->data = 'DB 1'; $this->Contacts. Table->rows = 10; $this->Map = $this->create('Google_Map', 'Map'); $this->Add. Button = $this->create('Button', 'Add. Button'); $this->Add. Button->text = "Add to favorites"; } } $page = new My_Page(); Rich Internet Applications with PHP 6/16/2021 29
Demo – Server code - presentation <html> <head><title>Zend Components Demo App</title> <link type="text/css" rel="stylesheet" href="<? = $page->static. URL('demo. css') ? >" /> <? $page->head(); ? ></head> <body> <? $page->DB 1 ->show(); ? > <div id="page"> <div id="header"><h 1>Zend Components Example App</h 1></div> <h 2>Contacts</h 2> <div id="Contacts. Table" class="box"> <? $page->Contacts. Table->show(); ? ></div> <span id="Add. Button"><? $page->Add. Button->show(); ? ></span> <div id="Map"><? $page->Map->show(); ? ></div> <script>…</script> </body> </html> Rich Internet Applications with PHP 6/16/2021 30
Component Rendering Rich Internet Applications with PHP 6/16/2021 31
Demo – Client code function mk. Url(data) { if(data[1]) { return ["/cm/getdata. php? id="+data[1][0]]; } } zc. Connect(ZC_Contacts. Table, "selected", ZC_Map, "address", mk. Address); zc. Connect(ZC_Contacts. Table, "selected", ZC_Address. Frame, "load", mk. Url); zc. Connect(ZC_DB 3, "datachanged", ZC_Favorites. Table, "refresh"); zc. Connect(ZC_Favorites. Table, "selected", ZC_Map, "address", mk. Address 2); zc. Connect(ZC_Clean. Button, "click", ZC_DB 3, "clean"); zc. Connect(ZC_Remove. Button, "click", ZC_DB 3, "remove", del. Fav); zc. Connect(ZC_Add. Button, "click", ZC_DB 3, "add", add. Fav); Rich Internet Applications with PHP 6/16/2021 32
Component Interaction Client – HTML page render AJAX call Component 1 Server-Side Bus Component 2 JS Event Component 2 Component 3 render JS Event render Component 1 Method Calls Component 3 Server – PHP page Persistent state Rich Internet Applications with PHP 6/16/2021 33
Interaction Scenario Client – HTML page Add button add Data Store update Favorites Table *updated!* Click! Add button add. Data Store store load. Data get. Data Favorites Table Server – PHP page Persistent state Rich Internet Applications with PHP 6/16/2021 34
Zend Component Model - Future • • • Component nesting Drag & drop support Visual event wiring Event standardization Tools for extending components Rich Internet Applications with PHP 6/16/2021 35
Thanks! Stanislav Malyshev stas@zend. com Please send feedback! http: //devzone. zend. com/article/2442 -PHP-and-RIA Copyright © 2007, Zend Technologies Inc.
- Slides: 36