Vork is an open-source PHP framework designed for rapid development of performance-oriented scalable applications.
The mission of Vork is to provide an MVC architecture and full-featured toolkit in a gimmick-free no-frills approach without adding overhead, creating slow & unscalable abstraction layers or re-inventing native PHP functionality.
Yii PHP Framework实用入门教程 by 周公的专栏
说明:因为最近工作工作关系,需要开发一个在Linux下运行的Web Application,需要对现在比较流行的一些PHP框架做一个了解和评估,下面的这篇文章是笔者最近学习一个比较新的PHP Framework的一点经历和操作步骤,因为官方的手册写得比较晦涩(特别是中文的),曾经尝试遍读它那个手册再动手,读了一大半发现仍无法理解,于是干脆先下手为强了,因而也就有了下面的文章。
介绍
Yii 是一个基于组件、纯OOP的、用于开发大型 Web 应用的高性能 PHP 框架。它将 Web 编程中的可重用性发挥到极致,能够显著加速开发进程。Yii适合大流量的应用,如门户、BBS、CMS及B2B系统等,功能丰富,性能优异,不过它的中文文档还不完善,并且有些命令行的操作是针对非Windows用户的,不易理解,所以制作了这篇文档。
下载地址:http://www.yiiframework.com/download/
中文文档地址:http://www.yiiframework.com/doc/guide/zh_cn
配置
下面针对本人的机器相关软件环境及路径做下说明:
Apache2.2.4+PHP5.2.5+MySQL5.1.39
在这里需要说明的是需要在设置Windows环境变量,在Path中添加PHP运行环境所在的目录(如本人在原有配置后加上”;C:\PHP”),因为使用Yii时需要PHP的运行环境。此外,在PHP版本选择时建议不要选择比较高的版本,本人是从PHP5.3.0->PHP5.2.11->PHP5.2.5一路降下来才运行成功的,建议尽量暂时不要使用PHP5.3.0、PHP5.2.11,本人在使用这两个版本过程中经常遇到一个ext目录下的dll文件不能加载的情况,当然你确认自己对PHP的配置相当熟悉的话例外。
因为在Yii中需要开启pdo和pdo_mysql,所以请确保在运行环境所使用的php.ini中取消了extension=php_mysql.dll、extension=php_pdo.dll、extension=php_pdo_mysql.dll的注释。
Apache的网站根路径为D:\wwwroot,在这个根路径下创建一个名为YiiDemo的文件夹,将从网上下载到的Yii压缩包解压之后,拷贝进D:\wwwroot\YiiDemo文件夹,文件结构如下:
注:上图中demos、framework、requirements是Yii压缩包中的文件夹,其它文件和文件夹是本人使用Eclipse时创建的。此外,在D:\wwwroot\YiiDemo\framework文件夹下有一个yiic.bat文件,这个文件可以帮助我们快速生成网站架构和MVC相关的文件。
另外,在本实例中MySQL和PHP都是用了utf8编码,不建议使用gb2312编码,能显示的中文字符太少,并且对其它东亚语系不支持,甚至连生僻点的繁体中文都不能显示,而utf8可以解决这个问题。
创建网站初始结构
启动Windows命令行程序(在开始菜单上找到“运行”,然后输入“cmd”并回车),可以看到如下命令行窗口:
在命令行方式下切换到Yii的framework目录下用以执行yiic命令(实际执行的是yiic.bat),如下:
看到如上图所示的信息之后,就可以使用yiic来创建网站结构了,在本例中我们在D:\wwwroot\YiiDemo\framework(注意Apache中网站根路径为D:\wwwroot)下创建网站,网站名字为study,先在D:\wwwroot\YiiDemo\framework下手动创建study这个文件夹,然后使用如下命令创建网站:yiic webapp 网站路径,如下图:
在输入创建网站的命令之后就会看到如上图所示的提示,键入”y”之后就会在D:\wwwroot\YiiDemo\study目录创建网站的框架结构,如下图所示:
按照本人机器配置,现在就可以看到Yii框架的雏形显示了,网址是:http://localhost/YiiDemo/study/index.php。
生成MVC文件
按照默认配置还不能使用数据库,要想获取与数据库的练习,需要更改配置,打开D:\wwwroot\YiiDemo\study\protected\config文件夹下的main.php文件,更改components中的配置即可,’db’这一参数被注释掉了,将’db’参数设置如下:
‘db’=>array(
‘connectionString’=>’mysql:host=localhost;dbname=study’,
‘username’=>’root’,
‘password’=>’jeri’,
),
保存之后即可连接MySQL数据库了,实际使用时要根据自己的实际情况配置。
在此将继续使用yiic这个命令行工具,通过cd切换工作路径到D:\wwwroot\YiiDemo\study这个文件夹,然后在命令行中可以创建model和view文件。如下:
需要注意的是,如上图所示,因为yiic.bat文件在D:\wwwroot\YiiDemo\framework文件夹下,而当前命令行的工作路径是D:\wwwroot\YiiDemo\study,所以在运行yiic时建议使用全路径。
使用yiic shell命令就可以进入shell命令行,在命令行看到的输入提示变为“>>”,键入model 表名就会创建对应表的model文件,上图下面就是使用“model user”成功之后就可以看到上图对应的情况。
还可以使用crud (crud分别是create/read/update/delete的缩写,表示常用的增删改查数据库操作)表名创建对应的coltroller和view文件,如下图所示:
比如针对表user、userlist等使用model和crud命令之后,我们就可以在浏览器中查看这些文件了,如查看userlist表中的数据,可以在浏览器中输入http://localhost/YiiDemo/study/index.php?r=userlist,看到如下所示的图:
可以看到尽管MySQL和PHP都是用了utf8编码,但是数据库中的RealName字段因为是中文数据导致不能正常显示,出现这种情况是因为采用了默认的字符集连接MySQL,在PHP中直接连接MySQL时,如果使用了utf8编码,我们会在PHP代码中做如下设置:mysql_query(”set names ‘utf8′”);但是在Yii中没有提供这样的机会,我们只需要更改D:\wwwroot\YiiDemo\framework\db文件夹下的CDbConnection.php文件中的
public $charset;
改为public $charset=’utf8′;就能正常显示了,如下图所示:
好了,至此没有写一行代码,但是已经完成了一个网站的大体框架,并且能初步实现增删改查功能了。
Hello, I want to introduce a new lightweight developer-centric PHP framework.
Full details and download is available at http://www.celeroo.com/frame/frame.html
Here is more about the features:
PHP4: YES, but will be dropped in the next update PHP5: YES MVC: YES Multiple DB’s: YES ORM: Open to integrate an ORM DB Objects: YES Templates: NO Caching: NO Validation: NO Ajax: Open to integrate with Protyotype, Jquery or any other (i.e. you can just use javascript) Auth Module: NO Modules: YES
Celeroo focus on rapid development and aims to remain simple and minimalistic. We will be adding new features and modules upon feedback, but the main criteria is easiness and improvement of development speed.
You think: “The APF is just another framework”, too? On this page you can find reasons why you should definitely use use the adventure php framework (APF)!
The APF supports you to rapidly create enterprise ready web applicatons or modules, that are fast, secure and reusable. The framework threrefore includes approved development tools to solve standard problems and implement pattern related applications.
The feature matrix gives you an overview of the features includes. It also compares the APF with other products.
The comparison indicators have the following meaning:
Within a discussion in the developers-guide.net forum, the idea of view based caching was born. Triggered by Alberto, we began to talk about performance optimization and various caching methods. The conclusion was, that caching of pieces of HTML is an effective way to gain performance.
This can be done by the cURL solution mentioned by Alberto or by view based caching. The benefit of view based caching method is, that the application itself must not be touched or adapted to the caching strategy. Within this article, I will talk about the idea of this technique and the implementation with the resources of the adventure php framework.
First of all, let me say a few words about the notion view. Spoken in APF terminology, a view is nothing else, than a template file included within another – maybe a layout template of your webpage. As described in the classes section, the presentation layer implementation of the APF features a generic page controller component, that analyzes APF style template files and generates a DOM tree out of the known tags.
Each tag consists of it’s tag definition – something like
<my:tag attr1=“value1″ attr2=“value2″ /> <my:tag attr1=”value1″ attr2=”value2″ />
and a class including the functionality of the tag. Each taglib must implemente the following methods, that are executed in the order presented in the code box:
class my_taglib_tag extends Document { function my_taglib_tag(){ } function onParseTime(){ } function onAfterAppend(){ } function transform(){ } }
To get a detailed idea, which functionality should be placed in which method, please have a look at the user specific taglibs tutorial.
So how can this fact be used to implement view based caching? Thanks to the generic DOM definition, the developer is able to implement own taglibs to enhance the core functionality or to satisfy the customer’s requirements. This means, that the existing template including mechanisms can be used as a basis for view based caching.
As described on the standard taglibs page, the <core:importdesign /> tag imports a template specified by it’s namespace and file name into the current DOM node. As I have mentioned above, this tag can be used to specify dedicated views within a layout template containg the header, footer or navigation functionality. Due to the fact, that we want to cache the content of special views, we can use this taglib as a basis.
To be sure about that, let us have a look at the taglib implementation:
class core_taglib_importdesign extends Document { function core_taglib_importdesign(){ parent::Document(); } function onParseTime(){ $Namespace = trim($this->__Attributes['namespace']); $Template = trim($this->__Attributes['template']); if(isset($this->__Attributes['context'])){ $this->__Context = trim($this->__Attributes['context']); } if(isset($this->__Attributes['incparam'])){ $IncParam = $this->__Attributes['incparam']; } else{ $IncParam = ‘pagepart’; } … $this->__loadContentFromFile($Namespace,$Template); $this->__extractDocumentController(); $this->__extractTagLibTags(); } }
The main functionality is to evaluate the tag’s attributes injected on parse time, to load and parse the content using the __extractDocumentController() and __extractTagLibTags() methods. The only thing, that is missing, is the caching part!
For caching purposes, the framework contains a flexible caching component with various backends. In this case, we simply use the text cache mechanism described in the text cache provider section in the documentation. In order to use it we have to provide a configuration section within the cache configuration file as described in the docs. The section might look like this:
[view_based_cache] Cache.Provider.Namespace = “tools::cache::provider” Cache.Provider.Class = “TextCacheProvider” Cache.Active = “true” Cache.BaseFolder = “/path/to/my/cache/base/folder” Cache.Namespace = “view::one”
To read and write the cache respectivly, we can use the following code fragment:
// get the cache manager $cM = &$this->__getServiceObject(‘tools::cache’,‘CacheManager’); $cM = &$cMF->getCacheManager(‘view_based_cache’); // calculate cache key $cacheKey = /* … */; // read the cache $cacheContent = $cM->getFromCache($cacheKey); // write to the cache if($cacheContent === null){ $cacheContent = /* generate content */; $cM->writeToCache($cacheKey,$cacheContent); }
To get things working, let’s put the pieces together:
class cache_taglib_importdesign extends core_taglib_importdesign { var $__CacheContent = null; function cache_taglib_importdesign(){ // call the parent’s constructor to fill the known taglib list parent::core_taglib_importdesign(); } function onParseTime(){ // get the cache manager $cMF = &$this->__getServiceObject(‘tools::cache’,‘CacheManagerFabric’); $cM = &$cMF->getCacheManager(‘view_based_cache’); // calculate the cache key $cacheKey = md5( $this->getAttribute(‘namespace’). $this->getAttribute(‘template’). get_class($this->__ParentObject) ); // try to read from the cache $this->__CacheContent = $cM->getFromCache($cacheKey); // check if the document was cached before. If not // execute the parent’s onParseTime() if($this->__CacheContent === null){ parent::onParseTime(); } } function transform(){ // generate the node’s output or return the cached content if($this->__CacheContent === null){ // get the cache manager $cM = &$this->__getServiceObject(‘tools::cache’,‘CacheManager’); $cM = &$cMF->getCacheManager(‘view_based_cache’); // calculate the cache key $cacheKey = md5( $this->getAttribute(‘namespace’). $this->getAttribute(‘template’). get_class($this->__ParentObject) ); // generate output and cache it $output = parent::transform(); $cM->writeToCache($cacheKey,$output); // return the tag’s output return $output; } else{ return $this->__CacheContent; } } }
As you can see, the onParseTime() and transform() methods are enhanced with the cache handling. To store the cache content and to be able to decide, if cached content is available, the private member variable __CacheContent was introduced. Please note, that the code duplication within the two methods is just there to illustrate the function flow.
The usage of the tag is not different to the usage of the <core:importdesign /> tag. The only difference is, that we have to provide a configuration section for the cache manager:
<core:addtaglib namespace=“” prefix=“cache” class=“importdesign” /> <cache:importdesign namespace=“my::namespace” template=“my_template” />
Thanks to the generic page controller implementation of the APF it is quite easy to implement a view based cache concept using taglibs. To have a robust and reusable taglib, the class printed above should have one or two more attributes, specifying the cache configuration section and perhaps the cache key. Doing so, the taglib is surely reusable within other projects or within several parts of your application.
Concerning the cache control, you have the choice to either clear the cache manually or use a cache provider, that can handle cache file life time. If you have the need, to add automatical refreshment, have a look at the enhancement chapter on the cache manager documentation page.
To try this out, I have created a sample implementation for PHP 5 including the enhancements written about in chapter 5. If you want to run the sample code, just download and extract the package view-based-caching-…-php5.zip into the DOCUMENT_ROOT of your webserver. Please be sure, that the user running your HTTP server has write permissions to the current folder. If you like to use another folder to store the cache, adapt the file /apps/config/tools/cache/sites/vbc/DEFAULT_cacheconfig.ini.