Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4999360
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2011-02-27 12:17:26

Zend Framework是php框架中的战斗机。各种功能应有尽有,没有的网上也能找到插件,helper, filter….而对于这样庞大的框架,最重要的就是配置了。
最近我作了一些尝试,在Zend Framework下使用AMF。现在把一些主要点共享给大家。
Tip 1,项目结构
这是刚接触ZF的同学们最头疼的。网上众说纷纭,其实很简单。
ZF发布的时候,bin目录下带有几个可运行的命令文件(包含各种系统了)。直接运行就可以了。
在这里,我是这样使用的(Mac Osx):
 
Yang:~ daniel$ ./zf.sh create project website

运行后,就会得到一个类似于这样的目录结构。
 
 
这里有个提示,尽管你可以把Zend文件夹(ZF的主要库文件)copy到刚才生成项目目录下的library里,我还是建议,一个服务器上维持一个ZF lib,这样升级起来容易。
现在可以把web服务器的服务地址只想我们生成目录bravo的public—这样,用户只能访问这个piblic目录中的文件,而程序文件是在application目录的,主要是安全。
好,打开浏览器,按照你配置的url来访问,应该能看到一个ZF生成的简单页面了。
Tip 2 index.php配置
ZF生成的public目录中,有一个index.php文件。这个文件是ZF应用的入口。这里的配置,主要是一些常量和ZF lib的位置。
比如我就是这样配置的:
  1. <?php
  2. // Define path to application directory

  3. defined('APPLICATION_PATH')
  4.     || define('APPLICATION_PATH',
  5.         realpath(dirname(__FILE__) . '/../application'));

  6. // Define application environment

  7. defined('APPLICATION_ENV')
  8.     || define('APPLICATION_ENV',
  9.               (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
  10.                                          : 'production'));
  11.                                          
  12. // Defile the path to the public

  13. define('PUBLIC_PATH', realpath(dirname(__FILE__)));

  14. //Add library directory to the include_path

  15. set_include_path(implode(PATH_SEPARATOR, array(
  16.     dirname(dirname(__FILE__)) . '/library',
  17.     get_include_path(),
  18. )));

  19. //这个是amf service的目录

  20. define('SERVICES_PATH', APPLICATION_PATH . '/services');

  21. define('APPLICATION_INI', APPLICATION_PATH.'/configs/application.ini');
  22. define('APPLICATION_INC', APPLICATION_PATH.'/data/cache/configCache.php');

  23. // Zend_Application

  24. require_once 'Zend/Application.php';

  25. // Create application, bootstrap, and run

  26. $application = new Zend_Application(
  27.     APPLICATION_ENV,
  28.     loadConfigCache(APPLICATION_INI, APPLICATION_INC, APPLICATION_ENV));

  29. $application->bootstrap()
  30.             ->run();


  31. //配置缓存处理函数返回路径

  32. function loadConfigCache($ini, $inc, $environment){
  33.     if (!file_exists($inc)) {
  34.         if ( !file_exists(dirname($inc)) || !is_writable(dirname($inc))) {
  35.                 throw new Exception('Specified file is not writeable (' .$inc.')');
  36.         }
  37.         
  38.         require_once 'Zend/Config.php';
  39.         require_once 'Zend/Config/Ini.php';
  40.         $config = new Zend_Config_Ini($ini, $environment);
  41.         $configs = '.PHP_EOL.'return '.var_export($config->toArray(), true).PHP_EOL.'?>';
  42.         file_put_contents($inc, $configs);
  43.     }
  44.     return $inc;
  45. }

Tip 3 配置文件
ZF生成的application.ini文件位于/application/configs/下, 这个目录也经常会放其他配置文件,如route,i18n等。
配置文件一定不能乱了。我们按照dev, prod两种环境来配置。
 
  1. [general]
  2. ;includePaths.library = APPLICATION_PATH "/../../library"
  3. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  4. bootstrap.class = "Bootstrap"
  5. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

  6. ;database resources.db.adapter = "PDO_MYSQL"
  7. resources.db.isdefaulttableadapter = true
  8. resources.db.params.driver_options.1002 = "SET NAMES UTF8;"

  9. [development : general]
  10. phpSettings.display_startup_errors = 1
  11. phpSettings.display_errors = 1
  12. resources.db.params.host = localhost
  13. resources.db.params.username = bravo
  14. resources.db.params.password = Z4wL6h8pA4KS5yDZ
  15. resources.db.params.dbname = bravo

  16. [production : general]
  17. phpSettings.display_startup_errors = 0
  18. phpSettings.display_errors = 0
  19. resources.db.params.host = 127.0.0.1
  20. resources.db.params.username = test
  21. resources.db.params.password = test
  22. resources.db.params.dbname = test
Tip 4 AMF 服务的编写
这个很简单。
不需要继承任何类。
顺便说说gateway的提供。ZF中,gateway是一个controller。里面的内容比较常规:
  1. <?php
  2. class GatewayController extends Zend_Controller_Action
  3. {
  4.            
  5.     public function init()
  6.     {
  7.         $this->getHelper('ViewRenderer')->setNoRender();
  8.     }

  9.     public function indexAction()
  10.     {
  11.         $server = new Zend_Amf_Server();
  12.         $server->setSession('Bravo');
  13.         Zend_Session::start();
  14.         
  15.         //让amf服务支持session

  16.         $server->addDirectory(SERVICES_PATH);

  17.         //index.php中声明的,服务所在的目录

  18.         //类的映射。

  19.         $server->setClassMap('com.bravo.model.User', 'Brava_Model_User');
  20.         echo($server->handle());
  21.     }
  22. }

 

Tip 5 Bootstrap
最主要的是初始化name space和数据库连接

  1. protected function _initAutoload()
  2. {
  3.     $autoloader = new Zend_Application_Module_Autoloader(array(
  4.     'namespace' => 'Bravo',
  5.     'basePath' => dirname(__FILE__),));
  6.     return $autoloader;
  7. }

  8. protected function _initExtraConfig()
  9. {
  10.     $resource = $this->getPluginResource('db');
  11.     $db = $resource->getDbAdapter();
  12.     Zend_Registry::set('db',$db);
  13.     Zend_Registry::set('configSection',APPLICATION_ENV);
  14. }

 

Tip 6 Value Object的传输
最让我头疼的。我现在的做法是,不使用ZF的Model, 直接用DbAdapter.
所以,我在Model里定义的其实是VO.
定义一个Base:

  1. <?php
  2. class Bravo_Model_Base
  3. {
  4.     public function setData($place){
  5.         foreach($this as $key=>$value){
  6.             if( array_key_exists($key, $place)){
  7.                 $this->$key = $place[$key];
  8.             }
  9.         }
  10.     }
  11. }

那么, User的Model就是:

  1. <?php
  2. class Bravo_Model_User extends Bravo_Model_Base
  3. {
  4.     public $_explicitType = 'com.bravo.model.User';
  5.     public $id;
  6.     public $email;
  7.     public $nickname;
  8.     public $passwd;
  9.     public $avatar;
  10. }

在从数据库中取得数据后:

  1. public function getPlace($i){
  2.     $db = Zend_Registry::get('db');
  3.     $select = $db->select();
  4.     $select->from('place')->where('place.id='. $i);
  5.     $p = $db->fetchRow($select);
  6.     $pvo = new Bravo_Model_Place();
  7.                  
  8.     //就可以这样了
  9.     $pvo->setData($p);
  10.     return $pvo;
  11. }

 

下一篇我们说客户端-应该是很简单了。

阅读(2481) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~