What’s in the Zend Framework for the Flash/Flex Developer?

The 1.7 release of the Zend Framework includes the new Zend_Amf package which provides a gateway server implementation for AMF remoting.  By the time of writing this article, there are not many resources available yet (a good starting point is here), and the few tutorials mostly guide you through the process of setting up the bootstrap file and establishing a MySQL database connection by using mysql_connect() directly in the service classes.

I would like to introduce a Zend AMF server setup that makes use of some of Zend’s best practices like configuration files, database adapters and models. If you are a Flash/Flex developer coming from an AMFPHP background and have never worked with the Zend framework before, you may easily feel overwhelmed by the framework’s complexity. This article is not meant to present a thourough overview of all Zend features you could possibly utilize in your Flex/Flash development work, but it might motivate you to dive deeper into the framework’s features.

The Folder Structure

First of all, even if I only need the AMF server from the Zend framework I set up my server directories like I would do with any other Zend/PHP application. If you already have some experience with the Zend framework this folder structure probably looks familiar to you:

This is a rudimentary folder structure, of course, because a typical Zend application with an HTML frontend has additional folders for controllers, views, layouts, forms, plugins, helpers, etc. But the basic setup is the same: We have an application and a library folder that are outside the web server’s root directory (public). The Zend framework packages are inside the library folder, and below the application folder there are a config and a models folder. For the special purpose of the AMF gateway, I added a services folder which is supposed to contain all service class files.

The Database and the Configuration File

Let’s assume we are working on an application that needs to manage user accounts. A very basic table would look like this:

CREATE TABLE users (
  1.   id int(11) NOT NULL AUTO_INCREMENT,
  2.   username varchar(32) NOT NULL DEFAULT '',
  3.   password varchar(32) NOT NULL DEFAULT '',
  4.   email varchar(255) NOT NULL DEFAULT '',
  5.   firstname varchar(50) NOT NULL DEFAULT '',
  6.   lastname varchar(50) NOT NULL DEFAULT '',
  7.   PRIMARY KEY (id)
  8. );

The database credentials are stored in an INI file (app.ini). In this example there are configuration data for both a production system and a development system. Because the development system configuration data are very similar to those for production, the development section inherits from the production section:

  1. [production]
  2. database.adapter = PDO_MYSQL
  3. database.params.host = localhost
  4. database.params.dbname = prod_dbname
  5. database.params.username = dbusername
  6. database.params.password = dbpassword
  7.  
  8. [development : production]
  9. database.params.dbname = dev_dbname

This configuration file also allows you to easily change the database adapter (if needed).

The Bootstrap File

Let’s take a look at the bootstrap file (index.php):

<?php
  1. define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
  2.  
  3. set_include_path(
  4.    get_include_path()
  5.    . PATH_SEPARATOR . APPLICATION_PATH . '/../library/'
  6.    . PATH_SEPARATOR . APPLICATION_PATH . '/services/'
  7.    . PATH_SEPARATOR . APPLICATION_PATH . '/models/'
  8. );
  9.  
  10. require_once 'Zend/Loader.php';
  11. Zend_Loader::registerAutoload();
  12.  
  13. $server = new Zend_Amf_Server();
  14. $server->setClass('UsersService');
  15. $server->setClassMap('UserVO','User');
  16. $server->setProduction(false);
  17.  
  18. echo($server->handle());

This bootstrap file is very similar to the standard bootstrap files you find in other tutorials or in the Zend Reference Guide. It adds some include paths, initializes the Zend autoload feature for classes, and sets up the AMF server.

The Service Classes

Utilizing the Zend_Config and Zend_Db packages, we set up the database connection in a base service class that the actual service classes inherit from. In line 7 in the listing below we provide a default database adapter for all subsequent instances of Zend_Db_Table objects in our application (for more information, please refer to the Zend_Db chapter in the Reference Guide). This way we keep the logic of creating a database connection in one central place.

<?php
  1. class BaseService
  2. {
  3.    public function __construct()
  4.    {  
  5.       $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', 'development');    
  6.       $dbAdapter = Zend_Db::factory($configuration->database);  
  7.       Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
  8.    }
  9. }

The only real service class in our example is UsersService which extends BaseService and invokes the parent class’s constructor by calling parent::__construct();. For the sake of simplicity it contains only one method (getUsers()):

<?php
  1. class UsersService extends BaseService
  2. {  
  3.    public function __construct()
  4.    {
  5.       parent::__construct();
  6.    }
  7.  
  8.    public function getUsers()
  9.    {
  10.       $usersArray = array();
  11.  
  12.       $table = new Users();
  13.       $result = $table->fetchAll();
  14.  
  15.       foreach($result as $row)
  16.       {  
  17.          $user = new User();
  18.          $user->id = $row->id;
  19.          $user->username = $row->username;
  20.          $user->email = $row->email;
  21.          $user->firstname = $row->firstname;
  22.          $user->lastname = $row->lastname;
  23.          array_push($usersArray, $user);
  24.       }
  25.       return $usersArray;
  26.    }
  27. }

You may wonder what the Users class does (line 12 in the above code listing). Here’s the answer: It’s a model class.

The Model Class

Models are another important concept of the Zend framework. Although the classical MVC pattern doesn’t apply in the context of the Zend AMF server, I stick to this concept here because it may come in handy sometimes. Imagine an application that is supposed to support both a Flash and an HTML interface. By following the framework’s MVC guidelines, we are able to build an application engine that uses the same model classes for both the AMF server gateway and the MVC-based HTML frontend.

The class Users is located in the models folder. It extends Zend_Db_Table and holds the name of the corresponding database table:

<?php
  1. class Users extends Zend_Db_Table
  2. {
  3.    protected $_name = 'users';
  4. }

Remember that we provided an application-wide database adapter for all Zend_Db_Table objects (see line 7 in the BaseService class). By creating an instance of Users (which inherits all methods from Zend_Db_Table) we are able to use fetchAll() to retrieve all rows from the table users.

The Value Object Class

The class User (see line 17 in the UsersService code listing) is a helper class that allows us to send an array of typed objects to the Flash player. This is especially useful in Flex-based applications where you want to use value objects for data-binding, for example. By the way, the class User doesn’t look any different than a VO class in an AMFPHP context.

<?php
  1. class User {
  2.    public $id = 0;
  3.    public $username = '';
  4.    public $password = '';
  5.    public $firstname = '';
  6.    public $lastnamne = '';
  7.    public $email = '';
  8. }

By calling $server->setClassMap('UserVO','User'); inside the bootstrap file, the User objects are mapped to UserVO.

I skip the Flex/Flash part here because this topic is well covered in other tutorials. But I hope you caught a glimpse of how you can leverage the power of other components inside the Zend framework - even if you are only interested in the Zend AMF server. There are more scenarios where the Zend framework may come in handy for a Flash/Flex developer (for example, think of generating PDF files on the fly by using Zend_Pdf).

Tags: , , , ,

9 Responses to “What’s in the Zend Framework for the Flash/Flex Developer?”

  1. Alan Says:

    This is great! Thanks for taking the time to post this.

  2. Karan Tandon Says:

    Thanks for doing this… i wish you the best for endeavours.
    :)

  3. diamondTearz Says:

    Thanks for posting this. I added it to my resources list. Take care.

  4. Business Entrepreneur Says:

    Great content! I just came across your blog and actually read your posts! I wish you would post more often. It is hard to find good informative blog like yours! Thanks for the information. - Versa

  5. Jenny R. Says:

    I was looking for blog ideas to add to my site and I found your site. I like what you have done and will be sure to check back for updates.

  6. Tim Reynolds Says:

    Nice post. Thank you for the info. Keep it up.

  7. Steve Says:

    This has been very helpful for me. Thanks a ton!

    I am interested in the very thing you suggested above. That is having two different front ends (HTML and Flex) for the application. My next task is understanding how to utilize the acl, auth, and sessions components for the Flex front end.

    Do you have any suggestions for that?

  8. Sander Lissenburg Says:

    Thnx for the info, this is just what I needed.

  9. Stefan Schmalhaus Says:

    I just added another post on how to set up a Zend AMF server inside a regular Zend controller. I think this is the best way of keeping the bootstrap file free from any AMF server-related code. Also, you gain a lot more flexibility when building applications with both HTML and Flex interfaces.

Leave a Reply