zend framework

41
Zend Framework Zend Framework 陈陈 陈陈 2008.10.22 2008.10.22

Upload: hao-chen

Post on 15-May-2015

3.448 views

Category:

Technology


3 download

DESCRIPTION

General introduction of Zend Framework, an OOP PHP5 Framework we use.

TRANSCRIPT

Page 1: Zend Framework

Zend FrameworkZend Framework

陈浩陈浩

2008.10.222008.10.22

Page 2: Zend Framework

陈浩 陈浩 haohappyhaohappy

Chinese Translator of:Chinese Translator of:

Programming PHPProgramming PHP 2 2ndnd edition edition 《《 PHPPHP 程序设计》第二版程序设计》第二版

PHP Objects, Pattens, and PracticePHP Objects, Pattens, and Practice 2 2ndnd edition edition 《《 PHPPHP 对象、模式与实践》第二版对象、模式与实践》第二版

Member of Zend Framework Documentation Team

(Team Lead of the Chinese Team)

Translator of PHP Manual

About the speakerAbout the speaker

Page 3: Zend Framework

PHP Objects, Patterns and PHP Objects, Patterns and FrameworksFrameworks

• PHP4PHP4 退出历史舞台,退出历史舞台, PHP5PHP5 成为主流成为主流• PHP5PHP5 对于面向对象的支持大大加强对于面向对象的支持大大加强

– 反射(反射( reflectionreflection )、异常()、异常( exceptionexception )、私有和保护()、私有和保护( privateprivate 和和 protectedprotected )的方法)的方法和属性、和属性、 __toString()__toString() 方法、方法、 staticstatic 修饰符、抽象类和抽象方法、修饰符、抽象类和抽象方法、 finalfinal 方法和属性、接口、方法和属性、接口、迭代器(迭代器( iteratoriterator )、拦截器()、拦截器( interceptorinterceptor )方法、类型检查、)方法、类型检查、 constconst 关键字、引用传递、关键字、引用传递、__clone()__clone() 方法和方法和 __construct()__construct() 构造方法等等。 构造方法等等。

• 面向对象的面向对象的 PHPPHP 开发 设计模式的应用开发 设计模式的应用• 面向对象的面向对象的 PHPPHP 开发框架开发框架

国内的 FleaPHP, ThinkPHP 等

Page 4: Zend Framework

Contents of this Contents of this presentationpresentation

• Brief Introduction of ZFBrief Introduction of ZF• MVC MVC • DatabaseDatabase• Useful libs 48 in totalUseful libs 48 in total

– Web2.0Web2.0 Dojo, Json, Soap, Rest, XmlRpc, AmfDojo, Json, Soap, Rest, XmlRpc, Amfnewnew, Services_, Services_*, Gdata*, Gdata

– Generic Generic Auth, Acl, Form, Layout, Cache, Config, PaginatoAuth, Acl, Form, Layout, Cache, Config, Paginator, Mail, Validate, Httpr, Mail, Validate, Http

Page 5: Zend Framework

MODELVIEW

CONTROLLER

场景:川菜馆

顾客看菜谱

顾客:来一盘鱼香肉丝

服务员:好咯

服务员告诉大厨:

2 号桌要一盘鱼香肉丝

大厨辛苦劳动。。炒炒炒

搞定。告诉服务员:炒好了。

服务员把菜端给顾客

MVC 的含义

Page 6: Zend Framework

Zend FrameworkZend Framework• 开放源代码的开放源代码的 PHP5PHP5 开发框架 开发框架 • 易于学习和使用易于学习和使用• 功能强大,促进快速开发功能强大,促进快速开发• 有活跃的开发者社区和完善的有活跃的开发者社区和完善的

支持平台支持平台• BSDBSD 许可 适合商业应用许可 适合商业应用• 由由 ZendZend 公司主导开发公司主导开发

合作:合作:

2008 Jolt Award

Page 7: Zend Framework
Page 8: Zend Framework

MVC in ZFMVC in ZFZend_Db_Table

Zend_View

Zend_Controller

Page 9: Zend Framework

单一入口:

index.php

MVC in ZFMVC in ZF

项目文件目录分布

Page 10: Zend Framework

Bootstrap (Bootstrap ( 入口文件入口文件 ))<?php <?php

// // 控制器实例化 控制器实例化 $controller$controller= = Zend_Controller_FrontZend_Controller_Front::::getInstancegetInstance(); (); $controller$controller->->setControllerDirectorysetControllerDirectory(($controllerDir$controllerDir); );

// // 设置路由 设置路由 $router $router = new = new Zend_Controller_Router_Route_RewZend_Controller_Router_Route_Rewriterite(); (); $controller$controller->->setRoutersetRouter(($router$router); );

// // 设置请求对象 设置请求对象 $request $request = new = new Zend_Controller_Request_HttpZend_Controller_Request_Http(); (); $controller$controller->->setRequestsetRequest(($request$request); );

$controller$controller->->throwExceptionsthrowExceptions((truetrue); ); $controller$controller->->returnResponsereturnResponse((truetrue); );

// // 得到响应对象 得到响应对象 $response $response = = $controller$controller->->dispatchdispatch(); ();

// // 输出 输出 echo echo $response$response; ; ?>?>

Page 11: Zend Framework

Zend_ControllerZend_Controller

Page 12: Zend Framework

Zend_ControllerZend_Controller用户访问: http://www.phpeye.com/article/view/id/1

Apache Server: url_rewrite

转向所有请求到入口文件 index.php index.php 初始化控制器

Router 对 URL 进行分解 根据路由规则,决定调用哪个 controller 中的哪个 action

Dispatcher 调用 controller 中的 action

Page 13: Zend Framework

Zend_ControllerZend_Controller• Zend_Controller_Front Zend_Controller_Front 设置设置 controllercontroller 目录,生成目录,生成 requestrequest 对象,并对象,并 dispatch()dispatch()

• Zend_Controller_Request_Abstract Zend_Controller_Request_Abstract 默认使用默认使用 Zend_Controller_Request_HttpZend_Controller_Request_Http

• Zend_Controller_Router_InterfaceZend_Controller_Router_Interface决定调用哪个决定调用哪个 ControllerController 中的哪个中的哪个 Action Action

• Zend_Controller_Dispatcher_InterfaceZend_Controller_Dispatcher_Interface调用调用 controllercontroller 中的中的 Action Action

Page 14: Zend Framework

Zend_ControllerZend_Controllerclass class ArticleController ArticleController extends extends Zend_Controller_Action Zend_Controller_Action { {

    public function     public function indexActionindexAction() ()     {     {     }     } public function public function viewActionviewAction()()

{{ $articleId = $this->request-> $articleId = $this->request->getParam(‘id’);getParam(‘id’); $article = $this->article->find($articleId); $article = $this->article->find($articleId); $this->view->article = $article; $this->view->article = $article; $this->view->render();// $this->view->render();// 不需要指定模板不需要指定模板 }}

}}

http://www.phpeye.com/article/ -> indexAction()

http://www.phpeye.com/article/view/id/1 -> viewAction()

Page 15: Zend Framework

Model - DBModel - DB

1. 数据库软件的抽象 基于 PDO (PHP Data Objects)

MySQL, OCI, PostgreSQL, ODBC, DB2, SQLite, Sybase/FreeTDS/MSSQL

2. SQL2. SQL 抽象抽象多个层次多个层次Zend_Db Zend_Db 手写手写 Sql $this->db->query($sql);Sql $this->db->query($sql);Zend_Db_Select Zend_Db_Select 部分手写部分手写Zend_Db_Table Zend_Db_Table 基本不用写基本不用写 SqlSqlZend_Db_Table_RowZend_Db_Table_Row

Row Data GatewayRow Data Gateway Zend_Db_Table_RowsetZend_Db_Table_Rowset

数据库访问抽象的两个层次

Page 16: Zend Framework

Table Data GatewayTable Data Gateway

An object that acts as a Gateway to a daAn object that acts as a Gateway to a database table. One instance handles all ttabase table. One instance handles all the rows in the table. he rows in the table.

Page 17: Zend Framework

Active RecordActive RecordAn object that wraps a row in a database An object that wraps a row in a database

table or view, encapsulates the database table or view, encapsulates the database access, and adds domain logic on that data. access, and adds domain logic on that data.

Page 18: Zend Framework

CRUDCRUD

Zend_Db_Table

$rows_affected = $this->insert($data);

$rows_affected = $this->update($data, $where);

$rows_affected = $this->delete($where);

$rowset = $this->find($id);

Zend_Db

$this->db->query($sql);

Page 19: Zend Framework

Zend_ViewZend_View• <html><html>• <head><head>• <title ><?php echo $title ?></title><title ><?php echo $title ?></title>• </head></head>• <body><body>• <?php echo $body ?><?php echo $body ?>• </body></body>• </html></html>

Page 20: Zend Framework

Zend_ViewZend_View• PHP is template language itself. You don’t need anPHP is template language itself. You don’t need an

other one. Just separate your logic from your preseother one. Just separate your logic from your presentation. ntation.

• View Renderer View Renderer • Compatible with other template systems, such as SCompatible with other template systems, such as S

marty, HTML_Template_PHPLIB, etc.marty, HTML_Template_PHPLIB, etc.

Page 21: Zend Framework

PHP & Web 2.0PHP & Web 2.0

Page 22: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0

Json, Soap, Rest, XmlRpc, AmfJson, Soap, Rest, XmlRpc, Amfnewnew, Dojo,, Dojo, GdataGdata

Zend_Services_* Zend_Services_* 针对某个特定网站的针对某个特定网站的 APIAPI 访问客户端访问客户端•Zend_Service_Akismet Zend_Service_Akismet •Zend_Service_Amazon Zend_Service_Amazon •Zend_Service_Audioscrobbler Zend_Service_Audioscrobbler •Zend_Service_Delicious Zend_Service_Delicious •Zend_Service_Flickr Zend_Service_Flickr •Zend_Service_Nirvanix Zend_Service_Nirvanix •Zend_Service_ReCaptcha Zend_Service_ReCaptcha •Zend_Service_Simpy Zend_Service_Simpy •Zend_Service_SlideShare Zend_Service_SlideShare •Zend_Service_StrikeIron Zend_Service_StrikeIron •Zend_Service_Technorati Zend_Service_Technorati •Zend_Service_YahooZend_Service_Yahoo

Page 23: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 Json

Built-in functions of PHP 5.2 + json_decode() json_encode()

Zend_Json

1. Json/PHP数据互相转换

2. 服务器端XML -> Json -> 输出到浏览器

3. JSON-RPC Server 同时支持Json-rpc v1, v2规范

Next

Page 24: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 Json

Back

Page 25: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 Json

// fromXml function simply takes a String containing XML contents as input.

$jsonContents = Zend_Json::fromXml($xmlStringContents, true);

Back

Page 26: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 Json

Back

Page 27: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 Gdatahttp://code.google.com/apis/gdata/

• Google Calendar • Google Spreadsheets • Google Documents List • Google Provisioning• Google Base • YouTube• Picasa Web Albums • Google Blogger • Google CodeSearch• Google Notebook

Zend_Gdata

Page 28: Zend Framework

Zend_Services_* Zend_Services_* 针对某个特定网站的针对某个特定网站的 APIAPI 访问客户端访问客户端•Zend_Service_Akismet Zend_Service_Akismet •Zend_Service_Amazon Zend_Service_Amazon •Zend_Service_Audioscrobbler Zend_Service_Audioscrobbler •Zend_Service_Delicious Zend_Service_Delicious •Zend_Service_Flickr Zend_Service_Flickr •Zend_Service_Nirvanix Zend_Service_Nirvanix •Zend_Service_ReCaptcha Zend_Service_ReCaptcha •Zend_Service_Simpy Zend_Service_Simpy •Zend_Service_SlideShare Zend_Service_SlideShare •Zend_Service_StrikeIron Zend_Service_StrikeIron •Zend_Service_Technorati Zend_Service_Technorati •Zend_Service_YahooZend_Service_Yahoo

Zend Framework & Web 2.0Zend Framework & Web 2.0

Page 29: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 REST

Zend_Rest_Client

Zend_Rest_Server

Page 30: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 REST

Zend_Rest_Client

$serverUrl = 'http://framework.zend.com/rest'; $client = new Zend_Rest_Client($serverUrl);

$result = $client->sayHello('Davey', 'Day')

->get();

if ($result->isSuccess()) {    echo $result; // "Hello Davey, Good Day"}

Page 31: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 REST

Zend_Rest_Client

/**

* 在 Technorati上查找某个博客的作者

*/

$technoratiUrl = 'http://api.technorati.com/bloginfo';

$technorati = new Zend_Rest_Client($technoratiUrl);$technorati->key($key);$technorati->url('http://pixelated-dreams.com');$result = $technorati->get();echo $result->firstname() .' '. $result->lastname();

Page 32: Zend Framework

Zend Framework & Web 2.0Zend Framework & Web 2.0 REST

Zend_Rest_Server

$server = new Zend_Rest_Server();$server->setClass('My_Service_Class');$server->handle();

Class My_Service_Class(){

public function sayHello(){

//return XML here

}

}

Page 33: Zend Framework

Zend_AmfZend_Amf支持 Adobe 的 Action Message Format (AMF)允许浏览器端的 Flash Player 与 PHP 进行通信 .

另外,可以将 Actions Script 中的数据(如对象)方便地转换成 PHP 中的数据。

PHP

Flash

Server

Client

Page 34: Zend Framework

Useful libsUseful libs• Auth, Acl, Form, Layout, Cache, ConfiAuth, Acl, Form, Layout, Cache, Confi

g, Paginator, Mail, Validate, Httpg, Paginator, Mail, Validate, Http

Page 35: Zend Framework

ZF & RoRZF & RoR

• Convention Over Configuration约定高于配置 Zend_View view rendererflexibility has a cost

• Dynamic Helpers For Views动态的 View Helper

• Generate Components For Application 自动生成系统部件 Zend_Tool

“I am a giant because I stand on the shoulders of others”

Page 36: Zend Framework

Drawbacks of ZFDrawbacks of ZF

1. Performance ?

2. Coupling ?

3. Complexity?

Page 37: Zend Framework

Too many files loaded in controllers?Too many files loaded in controllers?

Generated by PECL extentsion Inclued

Page 38: Zend Framework

Too many files loaded in controllers?Too many files loaded in controllers?

Xdebug + WinCacheGrind

Page 39: Zend Framework

Opcode cacheOpcode cache

• APC will be support in PHP6APC will be support in PHP6• eAcceleratoreAccelerator• XcacheXcache• Zend OptimizerZend Optimizer

Page 40: Zend Framework

QuestionsQuestions

Page 41: Zend Framework

ThanksThanks

MSN:

[email protected]