22
19/10/09 Building Web Applications with Zend Framework 1 Building Web Applications with Zend Framework

Building Web Applications with Zend Framework

Embed Size (px)

Citation preview

Page 1: Building Web Applications with Zend Framework

19/10/09 Building Web Applications with Zend Framework 1

Building Web Applications withZend Framework

Page 2: Building Web Applications with Zend Framework

What is Zend Framework?● A library of useful tools and classes?

● An MVC application framework?

● BOTH

Page 3: Building Web Applications with Zend Framework

Zend Framework Library● Yes, it's a little like PEAR● Huge active community● High quality code● Object Oriented PHP5● Extensively tested

Page 4: Building Web Applications with Zend Framework

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

Page 5: Building Web Applications with Zend Framework

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

Page 6: Building Web Applications with Zend Framework

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

Page 7: Building Web Applications with Zend Framework

MVC Dispatch OverviewRequest

Front Controller

ControllerPlugins

ControllerModels

ControllerAction Helpers

View

View Helpers

Layout

Response

Page 8: Building Web Applications with Zend Framework

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

Page 9: Building Web Applications with Zend Framework

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

Page 10: Building Web Applications with Zend Framework

Web Application in a Can

Page 11: Building Web Applications with Zend Framework

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.

Page 12: Building Web Applications with Zend Framework

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

Page 13: Building Web Applications with Zend Framework

Now for some of my favourites● ContextSwitch Controller Action Helper● AjaxContext Controller Action Helper● Zend_Form

Page 14: Building Web Applications with Zend Framework

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

Page 15: Building Web Applications with Zend Framework

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>

Page 16: Building Web Applications with Zend Framework

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”

Page 17: Building Web Applications with Zend Framework

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>

Page 18: Building Web Applications with Zend Framework

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

Page 19: Building Web Applications with Zend Framework

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

Page 20: Building Web Applications with Zend Framework

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'));

Page 21: Building Web Applications with Zend Framework

Zend_Form Example

Page 22: Building Web Applications with Zend Framework

Validators In Action