PHP Framework

浏览模式: 标准 | 列表 | 图文
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————
由于本文并没有什么特别的地方,而且单词也没有什么,故不作翻译。

Adobe to contribute AMF support to Zend Framework

Adobe to contribute AMF support to Zend Framework

Copy from framework.zend.com , it's submitted of Thursday, July 31, 2008
It's full contents :

Andi Gutmans, the CTO of Zend, blogged yesterday about a new proposal making it’s way through the Zend Framework process.
Adobe has made a proposal for an AMF (Action Message Format) component in Zend Framework. This ZF component will allow for client-side applications built with Flex and Adobe AIR to communicate easily and efficiently with PHP on the server-side. Leading the design of the component for Adobe is Wade Arnold. Wade already has a track record of bringing the Adobe RIA technologies to PHP as a result of all of his work on AMFPHP.
I know everybody over on the Zend Framework team is real excited about this proposal. If you work with Zend Framework then you are going to want to keep an eye on it.

and someone were comments for this news,

This is really great news! I am currently using JSON as the interface format between my Zend Framework and Flex app. This week I've been considering switching to amfphp (Lee Brimelow prepared a screencast http://www.gotoandlearn.com/player.php?id=78), but I didn't want to refactor/change all of my code. Zend_Amf is great news. I can't wait to begin using it. Cheers Adobe!

这样看来,以后ADOBE或许也会主动参与了吧?

ThinkPHP怎么样更好的使用Smarty第三方插件

如果你在使用ThinkPHP框架的时候不想采用TP自带的模版系统,而使用第三方的模版系统,你有很多其他的选择,在这里我仅介绍Smarty这种比较官方,而且比较强大的模版系统。

由于Smarty兼容PHP4,因此,它的效率会相对低一点点,这个低只是相对的,估计等Smarty啥时候正式放弃PHP4的时候,效率可能会上很大一个台阶。


官方标准使用方法:

在TP框架的PlugIns目录下面,有一个SmartTemplate目录,里面就是系统自带的Smarty插件。

使用方法如下:

1、在项目的Conf/Config.php文件里加上:

return array(
'THINK_PLUGIN_ON' => true,
'TMPL_ENGINE_TYPE'=>'smarty',
);

2、下载好Smarty,并将smarty的libs目录整个拷至项目的PlugIns目录下(说明一下,PlugIns目录可能会不存在,需要自己建立),同时将libs目录改名为SmartTemplate(希望没有记错,其实也就是与THINKPHP的PlugIns目录里的SmartyTemplate目录同名即可),如果你不愿意把目录改成这个名字,那么,你必须到TP的插件目录里修改插件文件,使之包含路径正确。

3、注意修改每次修改action或模板文件后删除Temp下的html文件

说明一下:上面的内容来自于官方,由lin_chaoqi朋友解答的,网址为:http://bbs.thinkphp.cn/viewthread.php?tid=305&highlight=smarty


在这里我要提的方法是于上面不一样的,黑黑

因为我在使用第三方模版插件的时候,特地看了TP的view.class.php发现了一些很重要的问题,那就是,如果采用第三方模版插件,那么第三方模版插件的效率可能不能保证,因为View类的fetch方法在判断是否为第三方插件之间,作了很多TP模版插件的自有处理,而这些对于使用第三方模版插件来说,几乎是完全无效的,这些处理可能会给第三方插件带来影响,同时也影响了第三方插件的执行效率。问题已经与流年沟通过,但由于改动可能会很大,或许最近几个版本里,流年都不会尝试作改进吧,一是怕影响了那些已经使用第三方插件的程序,二来如果去除掉这些处理,那么View类可能就不需要了。流年对于这样的情况应该是不愿意看到的。毕竟这也影响了原有系统的架构,估计流年得仔细考虑了……[当然从流年个人来说,肯定是希望大家都使用TP自有的模版插件,只是我目前对于smarty则是更加熟悉而己],但对于我这个使用者来说,我需要的是临时解决方法,所以,就有了下面的内容。

为了解决这个问题,我只有从View.class.php里下手,因为Action.class.php里有一行:

$this->tpl = View::getInstance();

那么,也就是说 tpl 这个变量是 View 的单例模式,检查了一下View.class.php中的这个getInstance方法,发现里面使用了 get_instance_of 这个函数(这个函数是有一点小BUG的,这里不作解释,但我目前也没有更好的解决方法),于是我对getInstance和__construct两个方法进行了改动,删除了__construct方法,加上了init方法,改动代码如下:

static function getInstance() {
return get_instance_of(__CLASS__,'init');
}

public function init ($type=''){
if(!empty($type)) {
$this->type = strToUpper( $type );
}else{
$this->type = strtoupper(C('TMPL_ENGINE_TYPE'));
}
if ( !in_array( $this->type, array('PHP','THINK') ) ){
$type = ucfirst( strToLower( $this->type ) );
vendor( $type );
return new $type();
}
return $this;
}

也就是让View类在实例化的时候,同时调用init方法。在这个方法里,我将我自己的模版插件放到了第三方插件的目录(Vendor)下。

切记切记:千万不可漏掉最后一句return $this;,其实这就是我所说的get_instance_of的BUG,如果不加这句,那么当type变量为PHP或THINK时,getInstance是无法返回实例的。

新的使用方法步骤如下:

1、修改项目的Conf/Config.php文件:

return array(
'THINK_PLUGIN_ON' => true,
'TMPL_ENGINE_TYPE'=>'TpSmarty',
);

2、在TP的Vendor目录下面,创建TpSmarty.php,内容如下:

<?php
include_once(PLUGIN_PATH."smarty/Smarty.class.php");

class TpSmarty extends Smarty {
public function __construct (){
parent::Smarty();
$this->caching = true;
$this->template_dir = TMPL_PATH;
$this->compile_dir = CACHE_PATH ;
$this->cache_dir = TEMP_PATH ;
}
}
?>

上面是最简单的写法,实际使用中,这些变量请改为与你自己的站点相配合。

3、根据上面的文件里的include_once函数,将smarty的libs目录拷贝至项目的PlugIns目录下,改名为:smarty(只需要与include_once中的目录相匹配即可)

4、然后,在项目的方法里就可以直接使用:

class IndexAction extends Action{
public function index(){
$this->assign('test','testss');
$this->display('default/index.html');
}
}

只是,使用了插件后,display方法的参数是模版的全路径,而且不能留空(并非不能解决,只是要改动的代码就会更多,目前这个方法是改动最少的)。

测试一下,是否正常了?呵呵 。

现在,我们把Config里的模版引擎换回Think,同时在Tpl/default/目录下建立Index目录,里面放上index.html,并修改上面的index()方法,将原来的$this->display('default/index.html'); 改为$this->display(); ,试一下,是不是也正常了?

测试一下:

<?php

if( !emptyempty( $test )){
echo 'test fck empty';
}
果然。不是我的错。。。哈哈是自动生成两个empty的。。。
为ThinkPHP开发自定义标签几个注意事顶

为ThinkPHP开发自定义标签几个注意事顶

ThinkPHP的模版引擎强大就强大在于它可以使用XML标签来为TP模版引擎进行扩展,因此TP的标签功能就成了TP模版的亮点所在。要想让TP的模版功能更加强大,你就得学会扩展TP的标签功能,即:自定义标签

开发自定义标签功能的时候,有几个注意的地方,
1、文件名的规范性,这个当然是不用多解释了,TagLibInput.class.php
(TagLib加是你定义的标签名,首字符大写,这样你的文件名中的Input就会自动认为你的XML文件名)
2、目录所在:目前暂时还是存放到THINKPHP的核心类库里,目录为:/THINKPHP/Lib/Think/Template
主程序文件放到:/THINKPHP/Lib/Think/Template/TagLib/ 目录下
自定义的XML文件放到:/THINKPHP/Lib/Think/Template/Tags/ 目录下

(自定义的文件是小写的标签名,即刚才类名中的Input的小写)

注意事顶:
如果你发现你自定义的标签没有被解析,请按如下方法进行检查(以input标签为例)
1、查看文件开始的地方是否加载了<tagLib name="cx,html,input" />,同时,请检查是否符合XML规范,如:标签一定要闭合。例:<br />,否则会出现XML解析错误
2、检查文件是否都为UTF-8编码,因为在解析XML的时候,都是采用UTF8编码,如果不是UTF8编码,会出现一些奇怪的解析错误,很多时候都会出现simpleXml解析器错误,如果出现这种错误,请检查文件编码,如果是UTF8编码的文件,请同时检查文件头是否存在BOM标记。
3、自定义的标签中,是否忘了双引号。由于在写HTML的时候,很容易忽略双引号,比如<input type=text name=name>,如果是这样,在被当成XML解析的时候,肯定会报错的,正确的写法应该是:<input type="text" name="name" value="value" />(标签要闭合),如果是自己自定义的标签,则应该这样写:<input:text name="" value="" />

解决使用ThinkPHP框架自带模版程序处理<??>出现的问题

解决使用ThinkPHP框架自带模版程序处理<??>出现的问题

使用ThinkPHP自带的模版引擎处理模版问题时,一般情况下不会有什么问题。

但是,在处理一些XHTML模版时,可能会遇到以下问题,比如,模版作者在制作模版时,为了规范页面,往往在文件头部加上<?xml version="1.0" encoding="utf-8"?>,表明文件是属于XML规范,所有的标签都必须遵循XML的相关规定。

然而,默认的ThinkPHP模版引擎在处理的时候会出现错误,处理这个错误的情况有以下几种办法。

第一种方法:你尝试关闭PHP的短标签功能
第二种方法:试着用<literal></literal>标签把XML的标签包含起来,即
<literal><?xml version="1.0" encoding="utf-8"?></literal>

第三种方法:修改ThinkPHP/Lib/Think/Template/ThinkTemplate.class.php的compiler函数,在函数的最后一行return $templContent;前加入

// 将<?标签用echo方式输出
$tmplContent = preg_replace('/(<?(?!php|=|$))/i', '<?php echo '\1'; ?>'."n", $tmplContent );

任何一种方法理论上都可以,第一种第二种方法实在不行的时候,再用第三种,毕竟第三种方法是改动核心的,如果以后更新核心库,可能会不当心就覆盖掉了。

不过,流年说过了,他已经更新了模板引擎的解析类,并且专门判断了是否开启了短标签,如果开启的话会自动用第三种方法解析。更新下SVN的ThinkTemplate.class.php 文件就可以了(可能压缩包还没有同步更新[2008-04-22])。

ThinkPHP两周年,同时有新的更新

新版介绍:

[ 1.0.2 ] 2008-2-21

  • Model类增加toArray方法
  • 修正因Log类的改动导致Trace错误信息无法正常显示的问题
  • 数据查询返回的数据集由ArrayObject对象改成数组
  • 修正Model类的create方法在定义字段映射的下面的bug
  • 修正虚拟模型下面create方法的bug
  • Model类的facade方法增加数据表字段的检测
  • 优化Image类的showAdvVerify方法
  • 修正标签库的compare标签
  • Trace配置文件由原来的_trace.php更名为trace.php
  • 项目调试配置文件由原来的_debug.php更名为debug.php
  • 项目配置文件由原来的_config.php更名为config.php
  • 路由定义文件由原来的_routes.php 更名为 routes.php
  • 静态定义文件由原来的_htmls.php 更名为 htmls.php
  • Model类的create方法无需type参数,自动判断新增和编辑模型数据
  • 增加第一次运行目录自动生成功能(只需要定义入口文件)
  • 默认项目编译缓存目录为Temp目录

78c8490a3b6bd1a40670ca61be3b8c95

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.

ThinkPHP介绍

ThinkPHP介绍

ThinkPHP 不是你所想象的只是一个提供一组类库的框架,ThinkPHP 是一个具有你开发所需的全功能的开发平台,是一个有机组合体,是一个让你可以立刻开始编写应用逻辑的开发框架,并且具有很好的扩展性。

[ 1.0.1 ] 2008-2-2

  • 修正Db类在Oracle下面的parseLimit方法判断
  • 优化数据库驱动类的查询结果获取
  • Model 类增加字段的表达式插入和更新支持
  • 完善了Db类的条件查询字段中带有空格的处理
  • Model类增加了delConnect方法用于删除动态增加的数据库连接
  • 增强了分布式数据库的支持 可以设置是否需要读写分离
  • Model类增加智能切换功能 switchConnect方法可以自动识别是否是相同的数据库连接类型
  • 增加了组件模块的URL分割定义配置 COMPONENT_DEPR 包括对操作链的设置采用相同的参数定义
  • 修正Model类的count等统计方法会自动缓存的问题 DB类默认关闭查询缓存
  • 修正RBAC组件的权限判断
  • 修正组件模块方式下面的模板文件/CMS/Tpl/default/Public的替换
  • 修正Html标签库的list标签的actionlist属性的支持
  • 修正PDO类在某些数据库下面的getAll方法的BUG
  • 增加核心缓存文件的开关功能 在入口文件里面设置 CACHE_RUNTIME 为 false
  • 修正了使用组件模块的时候模板文件中/index.php/Index的解析问题
  • 修正模板和语言的切换cookie
  • Model类增加addConnect和switchConnect方法 用于支持多数据库的连接
  • 修正了语言包的缓存导致切换语言无效的问题
  • Db类增加多数据库连接的内置支持
  • 修正Model类在某些数据库下面where条件表达式不支持where 1 的情况
  • Db类增加getLastSql方法用于获取最后一次查询的sql语句
  • 完善Log类的操作以及优化错误日志的写入
  • 修正model的数据库连接配置读取
  • 在Ajax返回之前保存日志记录
  • 完善compare标签
  • 改进Vendor函数的baseUrl参数定义
  • 改进项目语言包的定义 不同语言分成不同子目录
  • 简化了query方法的数据库缓存
  • 增加clearCache方法,用于清空项目相关缓存目录
  • 增加firebird数据库驱动支持
  • 修正pgsql驱动
  • 修正~app.php 文件的编译缓存路径的问题
  • 增加编译缓存路径的设置 RUNTIME_PATH

[ 1.0.0 ] 2007-12-25

  • 优化了执行效率和内存开销
  • Import方法增加同名检测和类检测
  • 完善PHP 5.2.0以下版本的支持
  • 去掉了核心对Config、Language、Cookie和Session类库的依赖
  • 去掉了Action类内置的默认操作
  • 改进了MySqli的驱动类库
  • 修正了主键不是自动增长的时候Model的add方法返回错误
  • 修正了mysqli的escape_string方法
  • 修正了PDO的连接params参数问题
  • 修正了Model的findAll的关联查询问题
  • 修正了Model类的getModelName方法问题
  • 修正了RBAC的模块大小写设置
  • 去掉volist resultset标签,均作为iterate标签的别名
  • 增加了项目编译机制和核心类库编译机制
  • 标签库定义增加标签的别名定义
  • XML模版结束标签增加对空格的支持
  • 增加compare标签和eq neq gt egt lt elt heq 和nheq标签别名
  • 增加了多模版和多语言的开关
  • RBAC认证增加了操作的认证设置,之前只支持模块的认证设置