标签:zend

浏览模式: 标准 | 列表 | 图文
Zend Studio 7.0 EA 已经发布

Zend Studio 7.0 EA 已经发布

从这个数字来看,确实是个颇具吸引力的东西啊。其他操作系统和更多版本下载请前往zend studio专门下载页面。
增加的新功能也是同样的引人瞩目。具体如下:

  1. Zend Studio 7.0 is built on top of the latest version of Eclipse (Galileo).[Zend Studio 7.0 基于Eclipse 的最新版本构建(Galileo).]
  2. PHP 5.3 Support[支持php5.3版本]
  3. Enhanced Source Code Editing[更强的源码编辑]
  4. Quick Root Cause Analysis through Zend Server Integration[集成zend server调试服务器]
  5. Rapid Application Development with Zend Framework[zf快速应用开发]
  6. Better Performance[更好的性能提升]

但是,由于这个是Early Access版本【早期使用版本?】,估计只适合体验尝新下,等最终的正式版本出炉了,我看才更有使用价值,喜欢的朋友可以下载的。

直接下载地址:http://downloads.zend.com/studio-eclipse/7.0.0/ZendStudio-7.0.0-EA.exe

原文来自gently的博客:http://www.zendstudio.net/archives/zend-studio-7-early-access/

Zend Framework 1.8.0 Released

1.8版本的公布,应该是ZF推动框架和代码标准化的步骤之一吧。随着加入ZF开发的人越来越多,ZF的功能越来越强大,但代码也越来越庞大了。

新版本主要是有两个特色功能:1是快速构建项目开发,利用cli程序来进行快速推荐,2是简化了云计算的操作(这主要应该是ZF和amazon的合作导致的吧?)

快速构建项目的功能,应该是比较让用户开心的,虽然zs6已经能够创建ZF项目,但还是有很多controller,action,model需要用户一一去创建,这,虽然不复杂,也如果多的话,也很繁琐。如今 ,1.8也让你解放出来了,看图上的命令行界面,你应该知道新版本是多么的方便了吧?(顺便说一下,现在国内的thinkPHP,QeePHP都有类似功能的工具推出,这可是国内走在了前端,他们都是一年前就有推出哦)

本次更新对Zend_Loader_Autoloader作出了更新,它与Zend_loader::autoload()不太一样,是一次新的改进。具体我也不翻译了,可以自己看一下代码就清楚了。这主要是对SPL的autoload所不能解决问题的一些改进,比如类名前缀等

云计算也是此次的亮点,但我一般用不了amazon的东西,所以我也不想翻译这些东西了。相信国内也用不了这些,哈哈

最后的更新,请看英文版最后的:Other Contributions,看看里面是不是有你想要的东西。

I'm pleased to announce the Zend Framework 1.8.0 release, the first in our 1.8 series of releases. This release marks the culmination of several long-standing projects, as well as a formalization of many of our recommended practices.

There are two major stories in this release: first, the addition of several components designed to provide and promote Rapid Application Development; second, two offerings that make using Zend Framework in the cloud easier.

Download it now!

Rapid Application Development

1.8.0 marks the first public release of Zend_Tool. At its simplest, Zend_Tool provides a command-line script that can be used to ease many common project-related tasks: setting up the project tree, adding controllers, actions (and related view scripts), model classes, etc. More advanced users can create their own tool providers that can then be directly invoked from that script -- or even create their own RPC endpoints so as to expose the tooling on the web or via a web service.

As an example of it in action, check out this screenshot:

zf.sh Help Screen

In a related vein, 1.8 also introduces Zend_Application, which provides a standard and object oriented method for bootstrapping applications. Bootstraps may define their own initialization resources, or draw upon some common plugins to do their work. Additionally, resources may be bootstrapped individually, allowing you to utilize a common bootstrap with multiple gateway scripts tailored for different tasks.

One dependency of Zend_Application is Zend_Loader_Autoloader. Zend_Loader_Autoloader is a replacement for Zend_Loader::autoload(), and solves many of the issues users have reported with that solution. Additionally, it provides the ability to manage a stack of namespaced autoloaders, working around some minor issues of the SPL's autoloader as well as providing opportunistic matching of namespace prefixes to match your classes quickly. A subcomponent, Zend_Loader_Autoloader_Resource provides a simple mechanism for mapping classes to the filesystem when the directory structure may not exactly correspond to the class name. By using an autoloader by default, you can help keep your code more performant as well as leave class resolution out of your code -- greatly epediting application development.

Note: the Zend Framework Quick Start has been updated to use Zend_Tool and Zend_Application. It's an excellent introduction to these components as well as Zend Framework's MVC in general.

Cloud Computing

Web 2.0 applications have many demands not seen in traditional web applications. One of these is the need for distributed storage, and another is the need to scale horizontally on demand as traffic to your site spikes.

Amazon has provided solutions to both of these problems for several years now with its Simple Storage Service (S3) and Elastic Compute Cloud (EC2), respectively.

Amazon S3 provides web services developers may use to store and retrieve data. The service is distributed, and thus highly scalable, reliable, and fast. Zend_Service_Amazon_S3 provides an object oriented approach to the service, as well as a PHP streams wrapper -- both designed to make working with S3 from your PHP applications a simple matter.

Amazon EC2 provides a web service to allow launching and managing server instances within Amazon's data centers. These server instances may be used at any time for any length of time -- allowing you to scale your site only when you need to handle extra traffic, or run your services entirely from the EC2 platform.

Other Contributions

The number of community contributions and bug fixes for 1.8.0 has been phenomenal. Below is a list of the primary feature additions for the release.

  • Zend_Tool, contributed by Ralph Schindler
  • Zend_Application, contributed by Ben Scholzen and Matthew Weier O'Phinney
  • Zend_Loader_Autoloader and Zend_Loader_Autoloader_Resource, contributed by Matthew Weier O'Phinney
  • Zend_Navigation, contributed by Robin Skoglund
  • Zend_CodeGenerator, by Ralph Schindler
  • Zend_Reflection, Ralph Schindler and Matthew Weier O'Phinney
  • Zend Server backend for Zend_Cache, contributed by Alexander Veremyev
  • Zend_Service_Amazon_Ec2, contributed by Jon Whitcraft
  • Zend_Service_Amazon_S3, Justin Plock and Stas Malyshev
  • Incorporated Dojo 1.3
  • Added support for arbitrary Dojo Dijits via view helpers
  • Zend_Filter_Encrypt, contributed by Thomas Weidner
  • Zend_Filter_Decrypt, contributed by Thomas Weidner
  • Zend_Filter_LocalizedToNormalized and _NormalizedToLocalized, contributed by Thomas Weidner
  • Support for file upload progress support in Zend_File_Transfer, contributed by Thomas Weidner
  • Translation-aware routes, contributed by Ben Scholzen
  • Route chaining capabilities, contributed by Ben Scholzen
  • Zend_Json expression support, contributed by Benjamin Eberlei and Oscar Reales
  • Zend_Http_Client_Adapter_Curl, contributed by Benjamin Eberlei
  • SOAP input and output header support, contributed by Alexander Veremyev
  • Support for keyword field search using query strings, contributed by Alexander Veremyev
  • Support for searching across multiple indexes in Zend_Search_Lucene, contributed by Alexander Veremyev
  • Significant improvements for Zend_Search_Lucene search result match highlighting capabilities, contributed by Alexander Veremyev
  • Support for page scaling, shifting and skewing in Zend_Pdf, contributed by Alexander Veremyev
  • Zend_Tag_Cloud, contributed by Ben Scholzen
  • Locale support in Zend_Validate_Int and Zend_Validate_Float, contributed by Thomas Weidner
  • Phonecode support in Zend_Locale, contributed by Thomas Weidner
  • Zend_Validate_Db_RecordExists and _RecordNotExists, contributed by Ryan Mauger
  • Zend_Validate_Iban, contributed by Thomas Weidner
  • Zend_Validate_File_WordCount, contributed by Thomas Weidner

In addition, we recorded well over 200 bug fixes for this release.

Please join me in a hearty round of congratulations and acknowledgment of all contributors who helped make this release possible through code contributions, feedback, testing, documentation, translations, and issue reports!

使用ZendFramework的时候打印SQL

使用ZendFramework的时候打印SQL

在使用ZendFramework开发的时候,会遇到要对SQL进行优化的处理,但是从ZEND的DB类里并没有找到输出SQL的方法,唯一看到的输出也是在SELECT类里面有一个__toString()的方法,但这个方法也只能输出SELECT的语句,对于Insert,Update,Delete之类的语句并不能处理。

开始的时候,很郁闷,后来问HaoHappy,因为他是国内第一批翻译ZF手册的人,果然我一问,他一下子就说,有一个Profiler类可以解决。
翻开手册,Zend_Db_Profiler,里面果然有操作。。(注:类名是凭记忆的,以下内容也是凭记忆写出来的)

1、在DB的CONFIG里面把profiler打开,如果没有打开,则需要用$profiler = $db->getProfiler();然后 $profiler->setEnable(true); (好象记错了,请看手册,我这里只说解决方法)

2、然后有一个设置 Profiler 的filter条件的,可以设定select,insert,update,delete的操作。

3、然后根据官方的例子,对SQL进行输出(手册上的例子,只是输出了执行时间最长的SQL哦)。如果需要每条都输出,你得在那个 if 判断外把sql 存储下来,即 $query->getQuery();

4、根据手册,还可以输出到fireBug和利用FirePHP来进行类似的debug,但我试了一下没有成功( QEE 支持 firePHP ,不知道是否借鉴了ZEND的这个功能,猜测而己 )

纯粹是一个记录,顺便感谢一下HaoHappy

ZF在ubuntu下的运行

ZF在ubuntu下的运行

ZEND FRAMEWORK要想正常的在ubuntu的AMP平台下运行 ,得打开apache的rewrite模块。这和windows下面把rewrite前的注释去掉可不一样。

进入命令行:a2enmod rewrite

会提示你成功打开rewrite模块

然后:

/etc/init.d/apache2 force-reload

重新加载一下,不过,这样还是不能成功的,因为默认的virtualhost中 有一个参数的值默认为NONE,要改成ALL

运行 :

vim /etc/apache2/sites-enabled/000-default

修改

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
把AllowOverride None 的 None 改为 All

然后

/etc/init.d/apache2 restart

OK,现在运行ZEND的例子,正常了吧,呵呵

zf的paginator的疑问

zf的paginator的疑问

如果按照ZEND的手册,在使用Zend_Paginator类的时候有两种方法,一种是直接new,一种是用factory
当然,用factory是很方便的。只需要Zend_Paginator::factory( $array )就行了
可是,如果这个array很大怎么办?它没有说。。
即使是new Zend_Paginator( Zend_Db_xxx ?$array )[不记得括号中是哪个abstract了],如果数据量大,他也没办法的呀??

今天白天我试着一下子查询了1W多条,立马死掉。
我看网上的例子,大多是要么直接传结果,要么是传 ZEND_DB的查询对象进去。但结果集都不大的,所以顺利运行 了。我不知道结果集是上万的时候会怎么样,呵呵

最后我是采用了先COUNT出总数,然后用range生成一个大数组传组 Zend_Paginator::factory() ,因为我看他里面的计数是用count的,所以我对factory的数组参数就直接传了这个range出来的数组给它。

结果,成功了,看来,这也就可以了。只是方法有点妖而己。。

记录一下,备忘一下。

Config文件
[paginator]
page.pagemax = 10
page.pagestart = 1
page.itemperpage = 25

在文件里

<?php
//...前面略
$request = $this->_request ;
$currentPage = $request->getParam('page');
$result = $table->fetchAll( ... 条件 );
$resultCount = $table->getCount( ... 条件 ); //文件自己实现
$resultCountArray = range( 1, $resultCount ); //到底是从0到resultCount-1好,还是直接从1到$resultCount好,我没有仔细测试过

$paginator = Zend_Paginator::factory( $resultCountArray );
$paginator->setCurrentPage( $currentPage )
->setItemPerPage( 25 ) //这个方法可能写错了,我是凭记忆写的,请看手册
->setPageRange( 10 );
$this->view->paginator = $paginator;

部分参数在获取失败的情况下,会直接从Config文件里读取,这里就不详细写了,大致应该是:

<?php
//.....略
$config = new Zend_Config_Ini('...path...' , 'paginator' ); //即Config中[]包含的文字

$pageMax = $config->page->pagemax ;
//取出来的每条数据都是对象了
继续解决ZendStudio5菜单中部分是方框的问题

继续解决ZendStudio5菜单中部分是方框的问题

昨天刚刚解决ZS白屏的问题,再次启动的时候,确实是正常了。。。
然而部份菜单却是方框,查了一些资料,发现是中文字体的问题,于是,将Windows下的宋体(simsun.ttc)拷贝过来,改名为: LucidaSansRegular.ttf,覆盖掉:/usr/local/Zend/ZendStudio-5.5.1/jre/lib/fonts 下的同名文件。

即:

unlink /usr/local/Zend/ZendStudio-5.5.1/jre/lib/fonts/LucidaSansRegular.ttf
cp /media/disk/WINDOWS/Fonts/simsun.ttc /usr/local/Zend/ZendStudio-5.5.1/jre/lib/fonts/LucidaSansRegular.ttf
來自風雪之隅的:深入理解PHP原理之foreach

來自風雪之隅的:深入理解PHP原理之foreach

原文地址:http://www.laruence.com/2008/11/20/630.html

膘叔的話:foreach是PHP語言里最常用的語法結構之一了,只要是數組操作,極大可能是會用到它,自從PHP5之后,它也能操作對象了。自此,foreach的使用率又上升了很多。但是有多少人知道它的背後原理呢?記得在以前的時候,好象是說foreach其實就是while( list )等的封裝。但由于一直沒有看過PHP的源碼也不能這么確定。(PS:如果用dezender等破解軟件破解PHP代碼,那么你會發現,代碼是foreach的那塊,都是被替換成while( list each )這種,所以我才會有上面的猜測)正好,看到風雪之隅有這個介紹,就轉貼一下。自己也可以學習一下。呵呵

foreach是PHP中很常用的一个用作数组循环的控制语句。
因为它的方便和易用,自然也就在后端隐藏着很复杂的具体实现方式(对用户透明)
今天,我们就来一起分析分析,foreach是如何实现数组(对象)的遍历的。
本节内容涉及到较多编译原理(lex and yacc)的知识,所以如果您觉得看不太懂,可以先找相关的资料看看。

我们知道PHP是一个脚本语言,也就是说,用户编写的PHP代码最终都是会被PHP解释器解释执行,
特别的,对于PHP来说,所有的用户编写的PHP代码,都会被翻译成PHP的虚拟机ZE的虚拟指令(OPCODES)来执行(参看:深入理解PHP原理之Opcodes).

不论细节的话,就是说,我们所编写的任何PHP脚本,都会最终被翻译成一条条的指令,从而根据指令,由相应的C编写的函数来执行。

那么foreach会被翻译成什么样子呢?

foreach($arr as $key => $val){
echo $key . '=>' . $val . "\n";
}

在词法分析阶段,foreach会被识别为一个TOKEN:T_FOREACH,
在语法分析阶段,会被规则:

unticked_statement: //没有被绑定ticks的语句
//有省略
|
T_FOREACH '(' variable T_AS
{ zend_do_foreach_begin(&$1, &$2, &$3, &$4, 1 TSRMLS_CC); }
foreach_variable foreach_optional_arg ')' { zend_do_foreach_cont(&$1, &$2, &$4, &$6, &$7 TSRMLS_CC); }
foreach_statement { zend_do_foreach_end(&$1, &$4 TSRMLS_CC); }
|
T_FOREACH '(' expr_without_variable T_AS
{ zend_do_foreach_begin(&$1, &$2, &$3, &$4, 0 TSRMLS_CC); }
variable foreach_optional_arg ')' { zend_check_writable_variable(&$6); zend_do_foreach_cont(&$1, &$2, &$4, &$6, &$7 TSRMLS_CC); }
foreach_statement { zend_do_foreach_end(&$1, &$4 TSRMLS_CC); }
//有省略
;

仔细分析这段语法规则,我们可以发现,对于:
foreach($arr as $key => $val){
echo $key . ‘=>’ . $val .”\n”;
}

会被分析为:

T_FOREACH '(' variable T_AS { zend_do_foreach_begin('foreach', '(', $arr, 'as', 1 TSRMLS_CC); }
foreach_variable foreach_optional_arg(T_DOUBLE_ARROW foreach_variable) ')' { zend_do_foreach_cont('foreach', '(', 'as', $val, ')' TSRMLS_CC); }
foreach_satement {zend_do_foreach_end('foreach', 'as');}

如果你懂语法分析,或者你懂
yacc,你会奇怪,为什么zend_do_foreach_cont的$6是$val 而不是 $key呢? 注意另外一个语法规则:
foreach_optional_arg:
/* empty */ { $$.op_type = IS_UNUSED; }
|
T_DOUBLE_ARROW foreach_variable { $$ = $2; }
;

也就是说,对于$key => $val的情况,yacc的内容栈对应的foreach_variable会被替换 {$$=$2};

然后,让我们来看看foreach_statement:
它其实就是一个代码块,体现了我们的 echo $key . ‘=>’ . $val .”\n”;
T_ECHO expr;

显然,实现foreach的核心就是如下3个函数:
zend_do_foreach_begin
zend_do_foreach_cont
zend_do_foreach_end

其中,zend_do_foreach_begin (代码太长,直接写伪码) 主要做了:
1. 记录当前的opline行数(为以后跳转而记录)
2. 对数组进行RESET(讲内部指针指向第一个元素)
3. 获取临时变量 ($val)
4. 设置获取变量的OPCODE FE_FETCH,结果存第3步的临时变量
4. 记录获取变量的OPCODES的行数

而对于 zend_do_foreach_cont来说:
1. 根据foreach_variable的u.EA.type来判断是否引用
2. 根据是否引用来调整zend_do_foreach_begin中生成的FE_FETCH方式
3. 根据zend_do_foreach_begin中记录的取变量的OPCODES的行数,来初始化循环(主要处理在循环内部的循环:do_begin_loop)

最后zend_do_foreach_end:
1. 根据zend_do_foreach_begin中记录的行数信息,设置ZEND_JMP OPCODES
2. 根据当前行数,设置循环体下一条opline, 用以跳出循环
3. 结束循环(处理循环内循环:do_end_loop)
4. 清理临时变量

当然, 在zend_do_foreach_cont 和 zend_do_foreach_end之间 会在语法分析阶段被填充foreach_satement的语句代码。

这样,就实现了foreach的OPCODES line。
比如对于我们开头的实例代码,最终生成的OPCODES是:

filename: /home/huixinchen/foreach.php
function name: (null)
number of ops: 17
compiled vars: !0 = $arr, !1 = $key, !2 = $val
line # op fetch ext return operands
-------------------------------------------------------------------------------
2 0 SEND_VAL 1
1 SEND_VAL 100
2 DO_FCALL 2 'range'
3 ASSIGN !0, $0
3 4 FE_RESET $2 !0, ->14
5 FE_FETCH $3 $2, ->14
6 ZEND_OP_DATA ~5
7 ASSIGN !2, $3
8 ASSIGN !1, ~5
4 9 CONCAT ~7 !1, '-'
10 CONCAT ~8 ~7, !2
11 CONCAT ~9 ~8, '%0A'
12 ECHO ~9
5 13 JMP ->5
14 SWITCH_FREE $2
7 15 RETURN 1
16* ZEND_HANDLE_EXCEPTION

我们注意到FE_FETCH的op2的操作数是14,也就是JMP后一条opline,也就是说,在获取完最后一个数组元素以后,FE_FETCH失败的情况下,会跳到第14行opline,从而实现了循环的结束。
而15行opline的op1的操作数是指向了FE_FETCH,也就是无条件跳转到第5行opline,从而实现了循环。

附录:

void zend_do_foreach_begin(znode *foreach_token, znode *open_brackets_token, znode *array, znode *as_token, int variable TSRMLS_DC)
{
zend_op *opline;
zend_bool is_variable;
zend_bool push_container = 0;
zend_op dummy_opline;

if (variable) {
//是否是匿名数组
if (zend_is_function_or_method_call(array)) {
//是否是函数返回值
is_variable = 0;
} else {
is_variable = 1;
}
/* 使用括号记录FE_RESET的opline行数 */
open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array));
zend_do_end_variable_parse(BP_VAR_W, 0 TSRMLS_CC); //获取数组/对象和zend_do_begin_variable_parse对应
if (CG(active_op_array)->last > 0 &&
CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode == ZEND_FETCH_OBJ_W) {
/* Only lock the container if we are fetching from a real container and not $this */
if (CG(active_op_array)->opcodes[CG(active_op_array)->last-1].op1.op_type == IS_VAR) {
CG(active_op_array)->opcodes[CG(active_op_array)->last-1].extended_value |= ZEND_FETCH_ADD_LOCK;
push_container = 1;
}
}
} else {
is_variable = 0;
open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array));
}

foreach_token->u.opline_num = get_next_op_number(CG(active_op_array)); //记录数组Reset Opline number

opline = get_next_op(CG(active_op_array) TSRMLS_CC); //生成Reset数组Opcode

opline->opcode = ZEND_FE_RESET;
opline->result.op_type = IS_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
opline->op1 = *array;
SET_UNUSED(opline->op2);
opline->extended_value = is_variable ? ZEND_FE_RESET_VARIABLE : 0;

dummy_opline.result = opline->result;
if (push_container) {
dummy_opline.op1 = CG(active_op_array)->opcodes[CG(active_op_array)->last-2].op1;
} else {
znode tmp;

tmp.op_type = IS_UNUSED;
dummy_opline.op1 = tmp;
}
zend_stack_push(&CG(foreach_copy_stack), (void *) &dummy_opline, sizeof(zend_op));

as_token->u.opline_num = get_next_op_number(CG(active_op_array)); //记录循环起始点

opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_FE_FETCH;
opline->result.op_type = IS_VAR;
opline->result.u.var = get_temporary_variable(CG(active_op_array));
opline->op1 = dummy_opline.result; //被操作数组
opline->extended_value = 0;
SET_UNUSED(opline->op2);

opline = get_next_op(CG(active_op_array) TSRMLS_CC);
opline->opcode = ZEND_OP_DATA; //当使用key的时候附属操作数,当foreach中不包含key时忽略
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
SET_UNUSED(opline->result);
}
void zend_do_foreach_end(znode *foreach_token, znode *as_token TSRMLS_DC)
{
zend_op *container_ptr;
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); //生成JMP opcode

opline->opcode = ZEND_JMP;
opline->op1.u.opline_num = as_token->u.opline_num; //设置JMP到FE_FETCH opline行
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);

CG(active_op_array)->opcodes[foreach_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); //设置跳出循环的opline行
CG(active_op_array)->opcodes[as_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); //同上

do_end_loop(as_token->u.opline_num, 1 TSRMLS_CC); //为循环嵌套而设置

zend_stack_top(&CG(foreach_copy_stack), (void **) &container_ptr);
generate_free_foreach_copy(container_ptr TSRMLS_CC);
zend_stack_del_top(&CG(foreach_copy_stack));

DEC_BPC(CG(active_op_array)); //为PHP interactive模式而设置
}
Zend_Acl and MVC Integration Part I (Basic Use)

Zend_Acl and MVC Integration Part I (Basic Use)

原文地址:http://devzone.zend.com/article/3509-Zend_Acl-and-MVC-Integration-Part-I-Basic-Use

原文内容:

By Aldemar Bernal

So, what is wrong with Zend_Acl and the current MVC implementation in the Zend Framework? there is nothing wrong, it is just that it gets not too obvious for developers how to achieve an optimal integration between these two important parts of the framework.

First at all, this article is based on the following Zend Framework Proporsal (link), by this moment this proposal is in Pending Recommendation state.

Well, how it works? There are two key components in this proposal:

  1. A Front Controller Plugin: This component resolves if the current user has access to the page which is being opened.
  2. An Action Helper: This component allows you to check whether the current user has access inside a controller.

Based on these two components, let's try them with an example. Let's talk about a website like DevZone, we would need a controller that work with the user management and another one which will deal with article management, as well we need 3 types of users (roles), one for guests, one for writers and another one which will approve the articles; resuming, we have:

Resources:

  1. user controller.
  2. article controller.

Roles:

  1. Guest.
  2. Writer.
  3. Admin.

Setting up the Zend_Acl component

After defined what we want to do, the next step will create a Zend_Acl instance which will reflect our model.

/** Creating the ACL object */
require_once 'Zend/Acl.php';
$myAcl = new Zend_Acl();

Creating the roles

Now we create the roles in our Zend_Acl instance.

/** Creating Roles */
require_once 'Zend/Acl/Role.php';
$myAcl->addRole(new Zend_Acl_Role('guest'))
->addRole(new Zend_Acl_Role('writer'), 'guest')
->addRole(new Zend_Acl_Role('admin'), 'writer');

Creating the resources

And then we create the resources needed (one per controller) and their relationship with the roles we created.

/** Creating resources */
require_once 'Zend/Acl/Resource.php';
$myAcl->add(new Zend_Acl_Resource('user'))
->add(new Zend_Acl_Resource('article'));

Creating the permissions

Now that we added the roles and resources to our Zend_Acl instance, it's time to explain what actions must be available to which roles.

  1. Guest won't have access to edit, add or approve an article.
  2. Writer won't have access to approve an article.
  3. Admin will have complete access.
/** Creating permissions */
$myAcl->allow('guest', 'user')
->deny('guest', 'article')
->allow('guest', 'article', 'view')
->allow('writer', 'article', array('add', 'edit'))
->allow('admin', 'article', 'approve');

Creating the access denied view file

We will need to create a view and an action which will address all those denied users, in order to do it, first we create a new action in our error controller:

class ErrorController extends Zend_Controller_Action
{
....

public function deniedAction()
{
}

....
}

And then we create our view file (/application/views/scripts/error/denied.phtml) with some warning message:

<h1>Error</h1>
<h2>Access denied</h2>
<p>You are trying to access an area which you have not allowed.</p>

Finishing the configuration

Okay, we have setup our Zend_Acl configuration, so far, it doesn't look like something new, but the next step is register the controller plugin, this important part takes the Zend_Acl instance we created and then validates it against the current page being accessed by an user.

/** Setting up the front controller */ 
require_once 'Zend/Controller/Front.php';
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory('path/to/controllers');

/** Registering the Plugin object */
require_once 'Zend/Controller/Plugin/Acl.php';
$aclPlugin = new Zend_Controller_Plugin_Acl($myAcl);
$aclPlugin->setRoleName($currentUserRole);

$front->registerPlugin(new Zend_Controller_Plugin_Acl($acl, 'guest'));

/** Dispatching the front controller */
$front->dispatch();

After this configuration is done, once an user enters in our application, depending the role he/she has the page will be displayed or an access denied page will be displayed.

For more information about this you can go to:
Zend_Acl & MVC Integration
and here is a small implementation source code of this:
Source Code

————END————
由于本文并没有什么特别的地方,而且单词也没有什么,故不作翻译。

ZendFramewok介绍

3a98d2de71841cd612748baced5a056b

ZF 其实我并不想多做介绍,毕竟对于熟悉PHP的人来说,ZF往往还是被当成研究对象的。官方出品的东西,怎么着都有其优点存在,而且有些类似于MFC中的隐藏函数功能,在ZF里也可能会看到,平时我们不太用、但确实有用的功能,也能够在ZF里面找到。

偷懒一下,直接把framework.zend.com中的whyzf的页面COPY过来,当作ZF的介绍,呵呵。

Why Zend Framework?

Extending the art & spirit of PHP, Zend Framework is based on simplicity, object-oriented best practices, corporate friendly licensing, and a rigorously tested agile codebase. Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services, and consuming widely available APIs from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and cataloguers like StrikeIron and ProgrammableWeb.

Expanding on these core themes, we have implemented Zend Framework to embody extreme simplicity & productivity, the latest Web 2.0 features, simple corporate-friendly licensing, and an agile well-tested code base that your enterprise can depend upon.

Extreme Simplicity & Productivity

We designed Zend Framework with simplicity in mind. To provide a lightweight, loosely-coupled component library simplified to provide 4/5s of the functionality everyone needs and that lets you customize the other 20% to meet your specific business needs. By focusing on the most commonly needed functionality, we retain the simplified spirit of PHP programming, dramatically lower the learning curve, and your training costs – so developers get up-to-speed quickly. We do this with:

  • An extensible and well-tested code base – easy to augment
  • A flexible architecture – not locked-in to a rigid application structure
  • No configuration files necessary to get up and running – or when maintaining and deploying your apps

Frameworks and best practices mean reduced training costs and quicker time-to-market – important factors in adoption decisions. Built so you can pick and choose just the pieces you need to turbocharge your web applications – all your developers know where to find their PHP / Zend Framework code, speeding new development and reducing maintenance costs.

Latest Web Development Features

  • AJAX support through JSON – meet the ease-of-use requirements your users have come to expect
  • Search – a native PHP edition of the industry-standard Lucene search engine
  • Syndication – the data formats & easy access to them your Web 2.0 applications need
  • Web Services – Zend Framework aims to be the premier place to consume & publish web services
  • High-quality, object-oriented PHP 5 class library – attention to best practices like design patterns, unit testing, & loose coupling

Friendly & Simple Licensing, Safe for the Enterprise

Based on the simple and safe new BSD license, with Zend Framework's License, you rest assured that your code is compliant, unimpeachable, and protected as you see fit. We also require all contributors to the open source Zend Framework to complete and sign a Contributor License Agreement (CLA) — which is based on the standard open-source Apache license — protecting your own intellectual property (that is, your added-value) built on Zend Framework.

Fully Tested – Extend Safely and Easily

Tested. Thoroughly. Enterprise-ready and built with agile methods, Zend Framework has been unit-tested from the start, with stringent code coverage requirements to ensure that all code contributed has not only been thoroughly unit-tested, but also remains stable and easy for you to extend, re-test with your extensions, and further maintain.