郭德纲的药方很有效果,5个土豆吃完还拉稀就再那个堵上,zend+flex

来源:互联网 发布:侠盗飞车mac版 编辑:程序博客网 时间:2024/04/20 06:54

以前有AMFPHP的很好用,最近在搞zend+flex的,E的以后在翻译吧

http://blog.log2e.com/2008/11/26/whats-in-the-zend-framework-for-the-flashflex-developer/

 

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

The 1.7 release of the Zend Framework includes the new Zend_Amfpackage which provides a gateway server implementation for AMFremoting.  By the time of writing this article, there are not manyresources available yet (a good starting point is here),and the few tutorials mostly guide you through the process of settingup the bootstrap file and establishing a MySQL database connection byusing mysql_connect() directly in the service classes.

 

I would like to introduce a Zend AMF server setup that makes use ofsome of Zend’s best practices like configuration files, databaseadapters and models. If you are a Flash/Flex developer coming from an AMFPHPbackground and have never worked with the Zend framework before, youmay easily feel overwhelmed by the framework’s complexity. This articleis not meant to present a thourough overview of all Zend features youcould possibly utilize in your Flex/Flash development work, but itmight 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 Zendframework I set up my server directories like I would do with any otherZend/PHP application. If you already have some experience with the Zendframework this folder structure probably looks familiar to you:

This is a rudimentary folder structure, of course, because a typicalZend application with an HTML frontend has additional folders forcontrollers, views, layouts, forms, plugins, helpers, etc. But thebasic 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 productionsystem and a development system. Because the development systemconfiguration data are very similar to those for production, thedevelopment 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 filesyou find in other tutorials or in the Zend Reference Guide. It addssome 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, weset up the database connection in a base service class that the actualservice classes inherit from. In line 7 in the listing below we providea 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. Althoughthe classical MVC pattern doesn’t apply in the context of the Zend AMFserver, I stick to this concept here because it may come in handysometimes. Imagine an application that is supposed to support both aFlash and an HTML interface. By following the framework’s MVCguidelines, we are able to build an application engine that uses thesame model classes for both the AMF server gateway and the MVC-basedHTML 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 UsersServicecode listing) is a helper class that allows us to send an array oftyped objects to the Flash player. This is especially useful inFlex-based applications where you want to use value objects fordata-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 coveredin other tutorials. But I hope you caught a glimpse of how you canleverage the power of other components inside the Zend framework – evenif you are only interested in the Zend AMF server. There are morescenarios where the Zend framework may come in handy for a Flash/Flexdeveloper (for example, think of generating PDF files on the fly byusing Zend_Pdf).

Tags: AMFPHP, Flash, Flex, Zend, Zend Amf

原创粉丝点击