Zend Framework is both a library of PHP classes and an MVC application framework. It includes tools for database access, caching, internationalization, and more. As an MVC framework, it uses conventions like controllers and views. Controller action helpers and view helpers provide reusable functionality. The framework supports plugins, custom routing, and context switching to determine the response format. Forms can be built from code or configuration and include validation, filtering, and internationalization.
Decarbonising Buildings: Making a net-zero built environment a reality
Building Web Applications with Zend Framework
1. 19/10/09 Building Web Applications with Zend Framework 1
Building Web Applications with
Zend Framework
2. What is Zend Framework?
● A library of useful tools and classes?
● An MVC application framework?
● BOTH
3. Zend Framework Library
● Yes, it's a little like PEAR
● Huge active community
● High quality code
● Object Oriented PHP5
● Extensively tested
4. 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
5. 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
6. 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
8. 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
11. Routes
Default 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.
12. 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
13. Now for some of my favourites
● ContextSwitch Controller Action Helper
● AjaxContext Controller Action Helper
● Zend_Form
14. 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
15. 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>
16. 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”
17. 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>
18. 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
19. 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