Zend Framework – ZF: Concepts, Methods and Examples

6 August, 2008 at 13:03 Leave a comment

ZF LogoZend Framework is an open source, object-oriented web application framework implemented in PHP 5 and licensed under the New BSD License. Zend Framework—often referred to as ZF—is developed with the goal of simplifying web development while promoting best practices in the PHP developer community.
ZF’s use-at-will architecture allows developers to reuse components when and where they make sense in their applications without requiring other ZF components beyond minimal dependencies. There is therefore no single development paradigm or pattern that all Zend Framework users must follow, although ZF does provide components for the MVC and Table Gateway design patterns which are used in most ZF applications. Zend Framework provides individual components for many other common requirements in web application development, including authentication and authorization via access control lists (ACL), application configuration, data caching, filtering/validation of user-provided data for security and data integrity, internationalization, interfaces to AJAX functionality, email composition/delivery, Lucene-format search indexing and querying, and all Google Data APIs along with many other popular web services. Because of their loosely coupled design, ZF components can be used relatively easy alongside components from other PHP web application frameworks.

Zend Framework is a simple, straightforward, open-source software framework for PHP 5 designed to eliminate the tedious details of coding and let you focus on the big picture. Its strength is in its highly-modular MVC design, making your code more reusable and easier to maintain. Although it’s currently in preview release, take a look—you may be surprised.

What is the Zend Framework, exactly? The Zend Framework:

  • Is based on PHP
  • Is object-oriented
  • Uses the MVC paradigm
  • Has open source contributors
  • Has contributors who take responsibility for the fact that their code is not the intellectual property of someone else

It also aims to make your programming life easier, not just in general, by instituting the MVC pattern, but also for specific things you tend to do all the time, like access databases or output to a PDF file. (OK — you probably don’t output to a PDF file all the time. But I’ll bet you would if it were easier.)

Zend Framework components include:

Zend_Controller
This module provides the overall control for the application. It translates requests into specific actions and makes sure they get executed.
Zend_Db
This is based on PHP Data Objects (PDO) and provides access to databases in a generic way.
Zend_Feed
This makes it easy to consume RSS and Atom feeds.
Zend_Filter
This provides string-filtering functions, such as isEmail() and getAlpha().
Zend_InputFilter
To Zend_Filter, this is designed to work with arrays such as form inputs.
Zend_HttpClient
This enables you perform HTTP requests easily.
Zend_Json
This enables you to easily translate PHP objects into JavaScript Object Notation, and vice-versa.
Zend_Log
This provides general-purpose logging functionality.
Zend_Mail
This enables you to send text and multipart MIME e-mail.
Zend_Mime
This is used by Zend_Mail to help decode MIME messages.
Zend_Pdf
This enables you to create new PDF documents, and load and edit existing PDF documents.
Zend_Search
This enables you to perform sophisticated searches on your own text. For example, you can build a search engine that returns results based on relevancy or other factors.
Zend_Service_Amazon, Zend_Service_Flickr, and Zend_Service_Yahoo
These provide easy access to these Web service APIs.
Zend_View
This handles the “view” portion of the MVC pattern.
Zend_XmlRpc
This enables you to easily create an XML-RPC client. (Server capabilities are planned for the future.)

Now let’s take a look at where we’re going and what we’re going to do.

Quick Start http://docs.thinkfree.com

Why Zend Framework?

Extending the art & spirit of PHP, Zend Framework is based on simplicity, object-oriented best practices, corporate friendly licensing, and a rigorously tested agile codebase. Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services, and consuming widely available APIs from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and cataloguers like StrikeIron and ProgrammableWeb.

Zend Framework Examples

Using the Zend Framework with Multiple Databases

September 12th, 2007 by Jaybill McCarthy

This article is what I hope will be the first in a series about my favorite way to develop web applications, the Zend Framework. I’m not going to try and sell the framework to you, I’m going to assume you already know all about it and that’s how you got here.

The Zend_Db_Table class provides a powerful and easy-to-use way of abstracting your data model into PHP objects. One problem I recently encountered was that the design of the class assumes you’re only going to use one database, which may not be the case.

Let’s assume you’ve got a Zend application that follows the de facto standard laid out in this tutorial. You’d have something in your application/config.ini file that looked like this:

  1. [general]
  2. db.adapter = PDO_MYSQL
  3. db.config.host = localhost
  4. db.config.username = zfuser
  5. db.config.password = ninjabunnies
  6. db.config.dbname = zftest
  7. db.config.port = 3306

Then in your index.php bootstrapper, you’d have something like this:

  1. Zend_Loader::loadClass(‘Zend_Controller_Front’);
  2. Zend_Loader::loadClass(‘Zend_Registry’);
  3. Zend_Loader::loadClass(‘Zend_Config_Ini’);
  4. Zend_Loader::loadClass(‘Zend_Db’);
  5. Zend_Loader::loadClass(‘Zend_Db_Table’);// load configuration
  6. $config = new Zend_Config_Ini(‘./application/config.ini’, ‘general’);
  7. Zend_Registry::set(‘config’, $config);
  8. // setup database
  9. $dbAdapter = Zend_Db::factory($config->db->adapter,
  10. $config->db->config->toArray());
  11. Zend_Db_Table::setDefaultAdapter($dbAdapter);
  12. Zend_Registry::set(‘dbAdapter’, $dbAdapter);

In the above example, we set all the database parameters in the config.ini file. We load these values into dbAdapter and decalre that adapter as the default via the setDefaultAdapter method of Zend_Db_Table. Any classes derived from Zend_Db_Table abstract will use this database parameter. This works fine for one database, but what if we’ve got more than one? We can’t really use the default adapter anymore, at least not in the usual way.

One way to get around this problem is to create an array of adapters and make the key of the array the name of the database. We can then implement an application specific table class that will allow us to easily choose the adapter we want to use. We can then have our application table classes extend that.

For this example, let’s say we’re building a site to sell textbooks, and there’s two databases: library and order_system

Your config.ini would now look something like this:

  1. [databases]
  2. db.library.adapter = PDO_MYSQL
  3. db.library.config.host = localhost
  4. db.library.config.username = libraryuser
  5. db.library.config.password = bunch0crunch
  6. db.library.config.dbname = library
  7. db.library.config.port = 3306
  8. db.library.default = truedb.order_system.adapter = PDO_MYSQL
  9. db.order_system.config.host = localhost
  10. db.order_system.config.username = ecommerceuser
  11. db.order_system.config.password = showMeThe$$$
  12. db.order_system.config.dbname = order_system
  13. db.order_system.config.port = 3306

The in your index.php bootstrapper, you would do something more like the following:

(more)

Building an AJAX Interface with Zend Framework

Earlier this year Zend announced an unprecedented partnership with the Dojo Foundation to deliver deeper integration between the Zend Framework and Dojo Toolkit projects. Although the ZF and Dojo communities will implement many integration points to make AJAX functionality available out of the box, each project plans to maintain compatibility with other server-side frameworks and client-side toolkits. In this webinar, we will explore several AJAX integration points made available by ZF that may be implemented for any toolkit with examples implemented using Dojo. (more)

Building RIA application with PHP and Zend Framework using AJAX

Zend Framework Session variable

Zend_Session_Namespace instances provide the primary API for manipulating session data in the Zend Framework. Namespaces are used to segregate all session data, although a default namespace exists for those who only want one namespace for all their session data. Zend_Session utilizes ext/session and its special $_SESSION superglobal as the storage mechanism for session state data. While $_SESSION is still available in PHP’s global namespace, developers should refrain from directly accessing it, so that Zend_Session and Zend_Session_Namespace can most effectively and securely provide its suite of session related functionality.

Each instance of Zend_Session_Namespace corresponds to an entry of the $_SESSION superglobal array, where the namespace is used as the key.


<?php
require_once 'Zend/Session/Namespace.php';

$myNamespace = new Zend_Session_Namespace('myNamespace');

// $myNamespace corresponds to $_SESSION['myNamespace']

It is possible to use Zend_Session in conjunction with other code that uses $_SESSION directly. To avoid problems, however, it is highly recommended that such code only uses parts of $_SESSION that do not correspond to instances of Zend_Session_Namespace. (more)

ZF, XML and JSON web services

Zend_Service_Delicious is simple API for using del.icio.us XML and JSON web services. This component gives you read-write access to posts at del.icio.us if you provide credentials. It also allows read-only access to public data of all users.

Get all posts


<?php
$delicious = new Zend_Service_Delicious('username', 'password');
$posts = $delicious->getAllPosts();

foreach ($posts as $post) {
    echo "--\n";
    echo "Title: {$post->getTitle()}\n";
    echo "Url: {$post->getUrl()}\n";
}


Retrieving posts

Zend_Service_Delicious provides three methods for retrieving posts: getPosts(), getRecentPosts() and getAllPosts(). All of these methods return an instance of Zend_Service_Delicious_PostList, which holds all retrieved posts.


<?php
/**
 * Get posts matching the arguments. If no date or url is given, most recent date will be used.
 *
 * @param string $tag Optional filtering by tag
 * @param Zend_Date $dt Optional filtering by date
 * @param string $url Optional filtering by url
 * @return Zend_Service_Delicious_PostList
 */
public function getPosts($tag = null, $dt = null, $url = null);

/**
 * Get recent posts
 *
 * @param string $tag   Optional filtering by tag
 * @param string $count Maximal number of posts to be returned (default 15)
 * @return Zend_Service_Delicious_PostList
 */
public function getRecentPosts($tag = null, $count = 15);

/**
 * Get all posts
 *
 * @param string $tag Optional filtering by tag
 * @return Zend_Service_Delicious_PostList
 */
public function getAllPosts($tag = null);

(more)

[wikipedia.org]
[http://www.zftutorials.com/]
[http://jaybill.com]
[http://framework.zend.com]
[http://www-128.ibm.com]

Bookmark and Share

Entry filed under: Ajax, JavaScript, JSON, PHP, XML, Zend Framework. Tags: , , , , , .

Simple Object Access Protocol – SOAP: Methods, Concepts and Examples Swing Application Framework (JSR-296): new concepts, new features

Leave a comment

Trackback this post  |  Subscribe to the comments via RSS Feed


IT Passion’s Store

Archives

Communities

Get the Source
OSGi supporter
JUG Milano

Upcoming Events



....

Blog Stats

  • 424,877 hits

My PageRank

What's My Google PageRank?