郭德纲的药方很有效果,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:
- id int(11) NOT NULL AUTO_INCREMENT,
- username varchar(32) NOT NULL DEFAULT '',
- password varchar(32) NOT NULL DEFAULT '',
- email varchar(255) NOT NULL DEFAULT '',
- firstname varchar(50) NOT NULL DEFAULT '',
- lastname varchar(50) NOT NULL DEFAULT '',
- PRIMARY KEY (id)
- );
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:
- [production]
- database.adapter = PDO_MYSQL
- database.params.host = localhost
- database.params.dbname = prod_dbname
- database.params.username = dbusername
- database.params.password = dbpassword
- [development : production]
- 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):
- define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
- set_include_path(
- get_include_path()
- . PATH_SEPARATOR . APPLICATION_PATH . '/../library/'
- . PATH_SEPARATOR . APPLICATION_PATH . '/services/'
- . PATH_SEPARATOR . APPLICATION_PATH . '/models/'
- );
- require_once 'Zend/Loader.php';
- Zend_Loader::registerAutoload();
- $server = new Zend_Amf_Server();
- $server->setClass('UsersService');
- $server->setClassMap('UserVO','User');
- $server->setProduction(false);
- 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.
- class BaseService
- {
- public function __construct()
- {
- $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/config/app.ini', 'development');
- $dbAdapter = Zend_Db::factory($configuration->database);
- Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
- }
- }
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()
):
- class UsersService extends BaseService
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function getUsers()
- {
- $usersArray = array();
- $table = new Users();
- $result = $table->fetchAll();
- foreach($result as $row)
- {
- $user = new User();
- $user->id = $row->id;
- $user->username = $row->username;
- $user->email = $row->email;
- $user->firstname = $row->firstname;
- $user->lastname = $row->lastname;
- array_push($usersArray, $user);
- }
- return $usersArray;
- }
- }
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:
- class Users extends Zend_Db_Table
- {
- protected $_name = 'users';
- }
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.
- class User {
- public $id = 0;
- public $username = '';
- public $password = '';
- public $firstname = '';
- public $lastnamne = '';
- public $email = '';
- }
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
- 郭德纲的药方很有效果,5个土豆吃完还拉稀就再那个堵上,zend+flex
- 有一个和尚负责做馒头,做好30个其它三个和尚就可以吃馒头, *当馒头吃完了,第一个和尚就再做30个馒头 生产消费
- android 实现listview 的item 出现在屏幕有个动画效果 类似于知乎的那个app
- 有个成名的美国作家写了本关于小米的书,还没翻译好世界就变了
- 网上找的约瑟夫环~~自己写的时候有个地方没想出来~~哎~~~~就出来了上面的那个笨方法~~~~
- 已经有13个球和一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次天平就找到那个球?
- 现换皮肤的效果,点击想要的颜色然后网页就换那个主题颜色
- 狗狗拉稀黑色的怎么办
- 土豆首页图片延迟加载的效果
- 两头堵模型:有一个字符串开头或结尾有n个空格,欲去掉前后空格返回一个新的字符串
- 这么多年的土豆都白吃了!土豆还能这么做,太香了
- 给医院开个药方
- 震惊世界的药方
- 刘道玉的三个“药方”
- 爱的药方
- 还记得移动的那个MV吗
- 雅虎天气的那个效果
- 药方
- 学习存储过程
- 处理 Java 程序中的内存漏洞(转)
- Flex开发:点对象添加与线对象的关系失败
- 超市自动扶梯吞进4岁娃成了植物人
- 宏定义的一些使用技巧总结
- 郭德纲的药方很有效果,5个土豆吃完还拉稀就再那个堵上,zend+flex
- Struts原理与实践
- MXML是什么?
- GIS研究方向
- 2008/4/23 关于事业的思考
- ms sql server 2005 安装在多核机器上,启动失败的问题
- swt 点击按钮往 table 中添加数据 tableEditor的使用
- 2008/4/24 《不对称创新》读后感
- 2008/9/27 采访稿:《开放API》