Upload
phil-brown
View
1.126
Download
1
Embed Size (px)
Citation preview
19/10/09 Building Web Applications with Zend Framework 1
Building Web Applications withZend Framework
What is Zend Framework?● A library of useful tools and classes?
● An MVC application framework?
● BOTH
Zend Framework Library● Yes, it's a little like PEAR● Huge active community● High quality code● Object Oriented PHP5● Extensively tested
Zend Framework Library● Advanced, loosely coupled toolset covering a
vast range of utilities, for example
Database Abstraction Table data gateway pattern Cache
Google Data API Adobe AMF Server HTTP Client
LDAP Email OpenID
PDF REST Client & Server Lucene Search
SOAP Internationalisation XMLRPC
Zend Application Framework● MVC Design Pattern● Rapid application development using
Zend_Tool command line utility● Convention over configuration defaults to a
somewhat verbose directory structure however flexibility allows developers choice
● Features include SEF URLS, custom routes, context switching and a strong plugin architecture
Zend Application Framework● Helper objects are one of the highlights of the
application framework● Controller Action Helpers for various controller tasks
– URL generation and redirection– Context Switching– Flash (single use) messaging
● View Helpers for visual / output encapsulation– HTML generation– Pagination– Navigation
MVC Dispatch OverviewRequest
Front Controller
ControllerPlugins
ControllerModels
ControllerAction Helpers
View
View Helpers
Layout
Response
Zend_Tool● Zend_Tool can be used to
● Create projects● Add modules, controllers, actions and views using
the default ZF directory structure● Show information about projects and the PHP
environment
Some ExamplesC:\Users\Phil\workspace>zf create project phpmelbCreating project at C:/Users/Phil/workspace/phpmelb
Starting a new project with Zend_Tool
Resulting directory structurephpmelb|-- application| |-- Bootstrap.php| |-- configs| | `-- application.ini| |-- controllers| | |-- ErrorController.php| | `-- IndexController.php| |-- models| `-- views| |-- helpers| `-- scripts| |-- error| | `-- error.phtml| `-- index| `-- index.phtml|-- library|-- public| `-- index.php`-- tests |-- application | `-- bootstrap.php |-- library | `-- bootstrap.php `-- phpunit.xml
Web Application in a Can
RoutesDefault route:controller/:action/:param/:value/:param/:value...
// or (for modular applications):module/:controller/:action/:param/:value/:param/:value...
Custom routes provide ultimate flexibility// typical “blog” route:year/:month/:title/:page=1
// with the following defaults:controller = “article”:action = “read”
A “module” is simply a collection of controllers, views and custom helper objects.
Plugin Architecture● Zend Framework supports customisation
through class extension, interface implementation and plugin loaders
● Plugin loaders let the framework know where to find custom classes and when to use them
● Controller plugins provide hooks for executing procedures at any stage in the dispatch cycle
Now for some of my favourites● ContextSwitch Controller Action Helper● AjaxContext Controller Action Helper● Zend_Form
ContextSwitch Controller Action Helper
● One controller action● Multiple view scripts depending on “context”● Actions may be assigned one or more contexts● Context specified on request using “format”
parameter● Built-in contexts include
● XML● JSON
● Developers may add custom contexts
ContextSwitch Helper Example// application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action{ public function init() { $this->_helper->contextSwitch->addActionContext('index', 'xml') ->initContext(); } public function indexAction() { $this->view->foo = 'foo'; }}
// application/views/scripts/index.phtml// Request example “/index/index”
<p>Foo: <?php echo $this->escape($this->foo) ?></p>`
// application/views/scripts/index.xml.phtml// Request example “/index/index/format/xml”
<root> <foo><?php echo $this->escape($this->foo) ?></foo></root>
AjaxContext Controller Action Helper
● Extends ContextSwitch Helper● Adds “html” context● Only fires on AJAX requests
● Detects using X_REQUESTED_WITH HTTP header
● Disables any layouts● Perfect for loading HTML “snippets”
AjaxContext Helper Example// application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action{ public function init() { $this->_helper->ajaxContext->addActionContext('index', 'html') ->initContext(); } public function indexAction() { $this->view->foo = 'foo'; }}
// application/views/scripts/index.phtml
<table> <tr> <td>Foo</td> <td><?php echo $this->escape($this->foo) ?></td> </tr></table>
// application/views/scripts/index.ajax.phtml// Only the output below is sent in the response
<td>Foo</td><td><?php echo $this->escape($this->foo) ?></td>
Zend_Form● One of the most powerful web application
development components● Combines
● Form HTML generation● Input filtering● Input validation● AJAX validation for client-side hooks● Internationalisation● Error handling and message display
Building Forms● Forms can be built in code or from config files
● XML● .ini● PHP Array
● Form appearance dictated by “decorators”● Decorators provide infinite markup possibilities
without effecting form logic
Zend_Form Example
$form = new Zend_Form;$form->addElement('text', 'foo', array( 'label' => 'Foo', 'required' => true, 'filters' => array('StringTrim')))->addElement('textarea', 'bar', array( 'label' => 'Bar', 'cols' => 60, 'rows' => 5, 'validators' => array( array('StringLength', false, array(0, 50)) )))->addElement('submit', 'submit_btn', array( 'label' => 'Submit'))->addDisplayGroup( array('foo', 'bar', 'submit_btn'), 'my_fieldset', array('legend' => 'My Form'));
Zend_Form Example
Validators In Action