PHP Framework

浏览模式: 标准 | 列表 | 图文
TP1.5版本中使用smarty模版引擎的技巧

TP1.5版本中使用smarty模版引擎的技巧

从TP1.5开始,对于其他的模版引擎有了原生支持(不再是以前那种插件机制了)。
本文以使用smarty模版为例作点简单介绍,其他的,可以参考一下View.class.php中的fetch方法可知。

TP的SVN中已经含有smarty模版库,因此当你要使用的时候,只需要在项目的config.php里作一点简单的配置:

'TMPL_ENGINE_TYPE' => 'smarty', //这个是设置引擎为smarty


从2009-01-07下午的SVN版本里,流年为又增加了一个TMPL_ENGINE_CONFIG这个数组,即:

'TMPL_ENGINE_CONFIG' = array(
'template_dir' => TMPL_PATH , //这个就是tpl目录了
'compile_dir' => CACHE_PATH . "tplCompile/", //这是我自己设定的,模版编译缓存放在这个目录里
'cache_dir' => CACHE_PATH . "tplCache/", //如果需要生成页面缓存,这个也是必须的
'left_delimiter' => '{',
'right_delimiter' => '}',
'caching' => false,
'force_compile' => true,
'compile_check' => true,
),


备注:如果按照我这样的写法,请到cache目录里手动创建tplCompile和tplCache两个目录,否则程序会报错。
报错信息大致为:

Catchable fatal error: Object of class Smarty could not be converted to string in D:\local\htdocs\ThinkPHP\Album\Temp\~runtime.php on line 145


如果出现这样的报错信息,请先检查这两个目录是否存在

在项目开发的时候,caching 最好设为false,否则你根本看不到效果。

如此设定完毕后,你就可以直接在项目中使用了,下面以Index模块的index方法进行举例:在IndexAction.class.php的index的方法里

$this->assign("test" , "This is a test string");
$this->display();

然后到模版里:

{$test}

就可以看到输出了。不过,这里需要注意的是,如果你$this->display()没有指定文件名,那么默认的模版文件就是default/Index/index.html,这点和原先使用TP默认的模版引擎没有什么区别。
出现问题最大的应该是在include方法里,include的使用方法是:{include file="Public/header.html"},就象我前面所说,smarty的模版路径只指到了tpl目录,但实际上,我们是在默认的default目录下操作,因此正确的写法应该是{include file="default/Public/header.html"}
如果我们写的程序要对应多模版,那么,上面那种直接写死default的方法是不行的,还好,TP为我们留了一个常量:TEMPLATE_NAME,于是我们的写法就可以是现在这样:

{include file="`$smarty.const.TEMPLATE_NAME`/Public/header.html"}

现在试一下,是否公用目录里的header.html被加载了?

最后再报一个warning。如果是在WINDOWS下面开发并且开启了DEBUG_MODE,那么你在读取模版的时候,页面的Trace信息里,偶尔会出现一个注意:

[ 09-01-08 14:36:02 ] 注意:[2] unlink(./Cache/tplCompile/\%%70^706^706C3AFE%%index.html.php) [function.unlink]: No such file or directory core.write_file.php 第 44 行.


这些信息,可以被忽略掉。模版文件还是会正常的生成和编译的。它只会在第一次生成模版编译文件的时候出现

QEE PHP 发布

QEEPHP终于发布了,没有看到廖羽雷吃笔记本,也是一种遗憾。

要知道QeePHP可是让人等了整整一年,当年论坛上就有人说:宁可相信世上有鬼也不相信老廖的嘴。可见让人有多失望

不过,在昨天凌晨的时候,QeePHP还是提供了下载,不过下载完了之后还没有时间仔细观摩。

初步看了一下,目录结构与ZF相类似,自动加载类库也使用了spl_auto_load,因为使用了这个函数,造成的后果是,文件名全部是小写了。呵呵(不知道原因的可以去查看这个函数)

其他还没有仔细看。

可以先看官方:http://qeephp.com

YY博鼎力之作:ThinkPHP 2008年离线资料包下载

YY博鼎力之作:ThinkPHP 2008年离线资料包下载

YY博鼎力之作哦,大家支持一下。
除此之外,他还写了一个yBlog,同样请大家支持
http://www.yhustc.com,就是yBlog的官方网站。

介绍:

ThinkPHP 2008年离线资料包,将ThinkPHP论坛使用版面与帮助资料版面2008年所有帖子全部采集生成HTML文件,并添加了全文检索功能。可以在不联网的情况下方便查找论坛中相关问题的讨论,并且可以方便的搜索,比CHM的搜索更强大的搜索。点击搜索结果可以直接来到帖子页,也可以直接通过首页进入相关版面列表,就跟操作BBS一样。

注意:需要安装.NET框架2.0及以上版本

图片:

9124f5e4e34cfc072c546ba0467dadd1

312e4ef8c7d2c7ee230926e0dc2e7c4f

0803a3f50241152d5897dfd27164fb25

下载地址:http://www.yhustc.com/ThinkPHP2008.rar

本站分流:下载

zendframework 1.6终于将SOAP放进去了

zendframework 1.6终于将SOAP放进去了

1.6版本的zf终于将SOAP放进来了,同时也增加了DOJO的支持。不过我没有想通。为什么那么多的JS框架,最终会选择了DOJO。

这些不是我能够想得通的。还是看看到底更新了什么再说吧。

An overview of new features:

  • Dojo Integration
    • JSON-RPC
    • Dojo Data packing
    • Dojo View Helper
    • Dijit integration with Zend_Form & Zend_View
    • Dojo Library Distribution
  • SOAP
    • SOAP Server
    • SOAP Client
    • Autodiscovery
    • WSDL access
    • WSDL Generation
  • Preview of Tooling Project in Laboratory (see /laboratory folder)
    • Command Line Interface
    • Project Asset Management
  • Unit Testing Harness for Controllers
  • Lucene 2.3 Index File Format Support
  • Zend_Session save handler for Database Tables
  • Paginator Component
  • Text/Figlet Support
  • ReCaptcha Service
  • Zend_Config_Xml Attribute Support
  • Character Set Option for DB Adapters
  • Zend File Transfer Component
  • New Media View Helpers (Flash, Quicktime, Object, and Page)
  • Support in Zend_Translate for INI File Format

This obviously marks a very important step towards a high-quality, highly tested 1.6 GA release. Thanks to everyone who has contributed to this release in any way: with patches/check ins, documentation/translations, and bug reports.

But our work is not yet over! Let's do our best to bring this release to the breaking point to find areas we can improve the release for General Availability. Based on your feedback we will determine in the next few weeks whether we require additional release candidates, so please provide feedback on our issue tracker (http://framework.zend.com/issues) as soon as you can and ask any questions/post your experiences on the appropriate mailing list.

Again, the Zend Framework community does NOT recommend this release for production use. We do, however, recommend evaluating new features in this release with existing and new applications.

Enjoy 1.6RC1, and see you on the issue tracker, wiki, and mailing lists!

————END————

不知道:Zend File Transfer Component,这个会给我们带来什么样的惊喜?大文件传输还是其他的?下载源码研究一下,哈哈

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或许也会主动参与了吧?

浅谈TP的COOKIE类

浅谈TP的COOKIE类

TP的COOKIE类是被封装好的,里面包含了一些常见的操作,set,get,clear等等,其实TP的cookie类与其他的COOKIE有着明显的不同,那就是TP的COOKIE ID是唯一的(不好意思,在我写完这段的时候,为了防止写错,我又重新打开了cookie.class.php,却突然发现,现在的cookie功能与以前的不一样了。)

以前的set函数是类似于这样:

$_COOKIE[C('COOKIE_ID')][C('COOKIE_PREFIX').$name] = $value ;
如今的又恢复成:
$_COOKIE[C('COOKIE_PREFIX').$name] = $value;

既然如此,我还是按最新的稍作讲解吧。。。

Cookie类,固名思意,就是把对COOKIE的操作进行了封装,通过定义一个COOKIE名的前缀,以防与其他COOKIE产生冲突,然后加上COOKIE变量的名称,就可以进行赋值、取值等。

Cookie类所有的操作都采用了静态方法,即在实际应用中,只要加载了Cookie类,就可以随时使用了:

<?
//假设cookie 前缀为 'neatcn_'
Cookie::get('test');
//return $_COOKIE['neatcn_test'];

Cookie::set('test','123456');
//return setcookie('neatcn_test','123456',xxx,xxx,xxx);
//xxx是默认定义的常量,如默认COOKIE的过期时间,作用域等
对于这些封装,可以方便使用,同样需要注意的是,在Cookie操作时,请不要输出header,否则可能会出现header already send的warning,同时无法写cookie,这点请需要注意。

另外一个需要注意的是,cookie在不同的浏览器下面有着不同的限制,最常见的就是cookie的长度,请尽量不要超过4K的字节。

参考资料:

* 一个 Cookie 档案最多只能包含 300 个 Cookies。(这只适用于 Netscape,因为它把所有的 Cookie 都放在一个档案。)
* 每一个 Cookie 的大小不得超过 4KB。
* 每一个 URL 路径,最多只能设定 20 个 Cookie。

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="" />