How to set up PHPUnit & XHProf for your Web Application

PHPUnit is a very great tool to test your web application. Really hate finding some mistake just before living my web application.So I guess if I could test my application asap by using some automatic test libs is very good.

Any way, let begin set up the enviroment first.

Very easy to download by using PEAR. Just follow the guide of PHPUnit document. Here is the link http://www.phpunit.de/manual/current/en/installation.html

Just pay attention to the path of you pear. If you install your php like

/somedisk/somepath/php/pear

You should use the correct path.

After installing PHPUnit. You can find phpunit in your path. If not, just set it up. Modify the ~/.bash_profile file. Add new PATH.

Done!!!

You can write you unit test case for you web application now. Just include the Autoload.php under PHPUnit folder.

XHProf is very cool tool . It is a function-level hierarchical profiler for PHP and has a simple HTML based user interface. Published by Facebook.com. Nice!!!

Download XHProf http://pecl.php.net/get/xhprof-0.9.2.tgz

tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib /somedisk/somepath/www/xhprof/
cd extension/
/somedisk/somepath/php/bin/phpize
./configure –with-php-config=/somedisk/somepath/php/bin/php-config
make && make install

Add blow to your php.ini

extension=xhprof.so
xhprof.output_dir=/somedisk.somepath/logs/xhprof

Reset your web server. That’s it.

Now put some code to your PHP files.

<?php
xhprof_enable();
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
$xhprof_on = true;
//your code......
if($xhprof_on) {
$xhprof_data = xhprof_disable();
$xhprof_root = '/somedisk/somepath/www/xhprof/';
include_once $xhprof_root . "xhprof_lib/utils/xhprof_lib.php";
include_once $xhprof_root . "xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "hx");
echo '<br /><a href="http://127.0.0.1/xhprof/xhprof_html/index.php?    run='.$run_id.'&source=hx" target="_blank">View Report</a>';
}
?>

Done & Enjoy!

How to install Ruby on rails on CentOS

First of all, You should have installed Database, I think Mysql is good. And you should already intalled gcc, make tools, and some other libs.

Here is a sample

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-serversYou probably need some source codes . Blow is a list:

Ruby , you can download it here. http://www.ruby-lang.org/

Ruby Gem, http://rubygems.org

It is quite simple to install Ruby on rails on CentOS.

Step 1, install ruby ,very simple
tar zxvf ruby-1.8.7-p174.tar.gz
cd ruby-1.8.7-p174
./configure
make && make install
Step 2, install ruby gem
tar zxvf rubygems-1.8.10.tgz
cd rubygems-1.8.10
./configure
make && make install
Step 3, install rails
sudo gem install rails

If you are in China , You should change your gem source. Taobao gives us a very nice solution.

Here is the link , http://ruby.taobao.org/ , just follow it to change the gem source.

Step 4, install Nginx and Passenger through gem
gem install passenger
passenger-install-nginx-module

Then follow the guide , download the newest version of nginx , then tell it where to install Nginx.
It will do it automaticly.

After this, you will get two line of nginx configuration, such as

passenger_root /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.13;
passenger_ruby /usr/local/bin/ruby;

it may a little different. like the passenger version is what you installed.

That’s all. Now it’s time to confige the Nginx host.

server {
    listen 80;
    server_name your_application_domain;
    root /your_application_path/public;
    passenger_enabled on;
    rails_env development;
}

Edit your host file , /etc/hosts.
Start nginx.

That’s all. Enjoy!

CentOS 下安裝Nagios(基本功能)md

本文摘自Nagios官方网站, 在CentOS下安装系统监控软件 Nagios

Introduction

This guide is intended to provide you with simple instructions on how to install Nagios from source (code) on Fedora and have it monitoring your local machine inside of 20 minutes.

No advanced installation options are discussed here - just the basics that will work for 95% of users who want to get started.

本指南提供给您如何在20分钟内基于Fedora平台从源码安装Nagios系统。未涉及到高级配置,但已满足95%的功能。

These instructions were written based on a standard Fedora Core 6 Linux distribution.

这些说明均基于标准的Fedora Core 6 发行版。

What You’ll End Up With
If you follow these instructions, here’s what you’ll end up with:

安装本指南,你将得到:

Nagios and the plugins will be installed underneath /usr/local/nagios
Nagios will be configured to monitor a few aspects of your local system (CPU load, disk usage, etc.)

The Nagios web interface will be accessible at http://localhost/nagios/
Nagios 和插件将被安装到 /usr/local/nagios 下 (注:此目录可以更改,根据用户要求安装在其他目录亦可以,注意前后统一即可)

Nagios将被监控本地系统的几个方面,例如 CPU负载,磁盘使用情况等等
可以通过http://localhost/nagios/来访问Nagios 的web 操作界面

Prerequisites

During portions of the installation you’ll need to have root access to your machine.

你必须拥有root权限。

Make sure you’ve installed the following packages on your Fedora installation before continuing.

确认系统内已安装下列安装包

Apache
PHP
GCC compiler
GD development libraries

You can use yum to install these packages by running the following commands (as root):

可以使用yum安装

yum install httpd php
yum install gcc glibc glibc-common
yum install gd gd-devel
1) Create Account Information

Become the root user.

成为root用户

su -l

Create a new nagios user account and give it a password.

建立一个名为 nagios 的用户,并且设置他的密码

/usr/sbin/useradd -m nagios

passwd nagios

Create a new nagcmd group for allowing external commands to be submitted through the web interface. Add both the nagios user and the apache user to the group.

建立一个名为 nagcmd 的用户组,将nagios 和apache 2个用户添加进这个用户组

/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
2) Download Nagios and the Plugins

Create a directory for storing the downloads.

建立下载后的临时目录

mkdir ~/downloads
cd ~/downloads

Download the source code tarballs of both Nagios and the Nagios plugins (visit http://www.nagios.org/download/ for links to the latest versions). These directions were tested with Nagios 3.1.1 and Nagios Plugins 1.4.11.

下载Nagios和 Nagios Plugins 的软件包。 本指南只用Nagios 3.1.1 和Nagios Plugins 1.4.11

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
3) Compile and Install Nagios

Extract the Nagios source code tarball.

解压缩Nagios源代码

cd ~/downloads
tar xzf nagios-3.2.3.tar.gz
cd nagios-3.2.3

Run the Nagios configure script, passing the name of the group you created earlier like so:

运行Nagios configure命令,并且将之前建立的nagcmd用户组设置为Nagios命令行运行组

./configure --with-command-group=nagcmd

Compile the Nagios source code.

编译源码

make all

Install binaries, init script, sample config files and set permissions on the external command directory.

安装2进制码,初始化脚本,示例文件,设置权限

make install
make install-init
make install-config
make install-commandmode

Don’t start Nagios yet - there’s still more that needs to be done…
注,此时不要启动nagios,还有很多工作要做

4) Customize Configuration

Sample configuration files have now been installed in the /usr/local/nagios/etc directory. These sample files should work fine for getting started with Nagios. You’ll need to make just one change before you proceed…

示例配置文件将被装在/usr/local/nagios/etc 下,这些配置文件可以另Nagios工作,但是需要更改一个地方

Edit the /usr/local/nagios/etc/objects/contacts.cfg config file with your favorite editor and change the email address associated with the nagiosadmin contact definition to the address you’d like to use for receiving alerts.

使用你喜欢的文本编辑器打开 /usr/local/nagios/etc/objects/contacts.cfg 文件。更改nagiosadmin 帐号 的email等相关信息

vi /usr/local/nagios/etc/objects/contacts.cfg
5) Configure the Web Interface

Install the Nagios web config file in the Apache conf.d directory.

安装Nagios的apache配置文件 ,此文件位于 apache的conf.d 目录

make install-webconf

Create a nagiosadmin account for logging into the Nagios web interface. Remember the password you assign to this account - you’ll need it later.

使用apache的htpasswd命令建立认证用户及密码

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Restart Apache to make the new settings take effect.

重启apache以使Nagios生效

service httpd restart

Note: Consider implementing the ehanced CGI security measures described here to ensure that your web authentication credentials are not compromised.

考虑到实现CGI安全的程度,请确保你的web认证证书受到危害

6) Compile and Install the Nagios Plugins

Extract the Nagios plugins source code tarball.

解压Nagios插件源码

cd ~/downloads
tar xzf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11

Compile and install the plugins.

编译安装插件, 注意运行的用户组

./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
7) Start Nagios

Add Nagios to the list of system services and have it automatically start when the system boots.

设置Nagios为系统服务,并且在系统启动时启动Nagios

chkconfig --add nagios
chkconfig nagios on

Verify the sample Nagios configuration files.

检验Nagios的配置文件

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, start Nagios.

如果没有错误,启动Nagios

service nagios start
8) Modify SELinux Settings

Fedora ships with SELinux (Security Enhanced Linux) installed and in Enforcing mode by default. This can result in “Internal Server Error” messages when you attempt to access the Nagios CGIs.
See if SELinux is in Enforcing mode.

Fedora 如果强制打开SELinux ,当访问Nagios 的CGI时,将会出现 “Internal Server Error” 错误,下面命令查看是否打开SELinux

getenforce

Put SELinux into Permissive mode.

设置禁用SELinux

setenforce 0

To make this change permanent, you’ll have to modify the settings in /etc/selinux/config and reboot.

需要永久禁用SELinux,请编辑 /etc/selinux/配置文件,之后重启。

Instead of disabling SELinux or setting it to permissive mode, you can use the following command to run the CGIs under SELinux enforcing/targeted mode:

也可以使用以下命令使CGI在SELinux的合法模式下运行

chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

For information on running the Nagios CGIs under Enforcing mode with a targeted policy, visit the Nagios Support Portal or Nagios Community Wiki.

如需要在ENLinux的强制模式下运行Nagios,请访问Nagios的WIKI

9) Login to the Web Interface

You should now be able to access the Nagios web interface at the URL below. You’ll be prompted for the username (nagiosadmin) and password you specified earlier.

现在你可以使用之前htpasswd建立的用户登录web界面的nagios

http://localhost/nagios/

Click on the “Service Detail” navbar link to see details of what’s being monitored on your local machine. It will take a few minutes for Nagios to check all the services associated with your machine, as the checks are spread out over time.

点击 “Service Detail”查看本地机器的细节部分,需要一定时间来让Nagios关联至本地机器的相关模块。

10) Other Modifications

Make sure your machine’s firewall rules are configured to allow access to the web server if you want to access the Nagios interface remotely.

确认防火墙规则。

Configuring email notifications is out of the scope of this documentation.

配置email

While Nagios is currently configured to send you email notifications, your system may not yet have a mail program properly installed or configured. Refer to your system documentation, search the web, or look to the Nagios Support Portal or Nagios Community Wiki for specific instructions on configuring your system to send email messages to external addresses. More information on notifications can be found here.

确认安装email组件,使之可以在系统发生状况时发送给你email,其他更多咨询,请查看Nagios的WIKI

11) You’re Done

Congratulations! You sucessfully installed Nagios. Your journey into monitoring is just beginning. You’ll no doubt want to monitor more than just your local machine, so check out the following docs…

祝贺你!你成功安装了Nagios。

MySQL 查询连接数

查看当前所有连接的详细资料:

./mysqladmin -uadmin -p -h10.140.1.1 processlist

只查看当前连接数(Threads就是连接数.):

./mysqladmin  -uadmin -p -h10.140.1.1 status

修改mysql最大连接数:

打开my.ini,修改max_connections=100(默认为100)。
命令

show processlist;

如果是root帐号,你能看到所有用户的当前连接。如果是其它普通帐号,只能看到自己占用的连接。
show processlist;只列出前100条,如果想全列出请使用show full processlist;

Unbuntu使用Nginx安装PHP ,Rails开发环境

Unbuntu是一个非常好的Linux发行版本,它可以作为一个桌面级的操作系统来进行工作,当然对于开发者来说,也是一个相当不错的开发环境,下面介绍一下我在Ubuntu系统下搭建的Php和Rails开发环境。

这种搭建方式,主要是使用Nginx作为前端服务器,使用spawn-fcgi来运行php服务,使用Mongrel 作为Rails的服务,之后Nginx只要监听次2个服务的端口就可以了,以此类推,可以适用于其他的Web服务器,只要安排好端口,防止冲突就可以了

首先,安装好Ubuntu系统,我使用的是10.04版,之后当然设置好源,本人使用163的源,很不错,相当快速。

第二步,安装必要的软件,如下:

sudo apt-get install nginx
sudo apt-get install mysql-server
sudo apt-get install php5 php5-cgi php5-mysql php5-curl php5-gd php5-xmlrpc
sudo apt-get install spawn-fcgi

注意,安装php的时候,依据个人需要,可以对所安装的库文件进行增减,具体请参照手册。

安装好软件包之后,Nginx就应该启动好了,打开Localhost,就可以看到默认页面。

第三步,进行一些必要的配置

Nginx的配置是少不了的,也非常简单,在 nginx的配置目录下有个default文件,就是它自带的配置,里面也有实例,这个就不多写了。

下面是启动Php的脚本示例

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u username -g username -f /usr/bin/php-cgi -C 20

一般情况下,spawn-fcgi被安装在/usr/bin下,所以只要设定启动端口(要和Nginx监听端口一致)就可以

-f 指定调用 FastCGI 的进程的执行程序位置,根据系统上所装的 PHP 的情况具体设置

这样一来,基本就完成了,可以运行PHP了,之后就是安装Eclipse

首先安装Sun的JDK,当然现在是Oracle的了, 只要下载bin文件,注意是32位还是64位,

之后设置成可运行,直接安装就可以

主要是要增加环境变量,如下

修改/etc/enviroment

设置path,java_home,classpath

PATH="......:/usr/lib/jvm/java/jdk1.6.0_12/bin"
CLASSPATH=.:/usr/lib/jvm/java/jdk1.6.0_12/lib
JAVA_HOME=/usr/lib/jvm/java/jdk1.6.0_12

以上/usr/lib/jvm/java是安装目录

再执行shell

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_12/bin/java 300
update-alternatives --install /usr/bin/javac javac /usr/lib/java/jdk1.6.0_12/bin/javac 300

将系统默认的java设置好,可以使用 java -version来测试一下

至于Rails也是如法炮制,直接apt-get rails mongrel 就可以安装了,主要是 Ngxin设置好监听,下面是例子

server {
    listen       80;
    server_name  sample.rails.com;
    root   /home/username/workspace/sample/public;
    index index.html index.htm;
    charset utf-8;
    access_log  /home/username/tmp/logs/sample.rails.com.access.log;
    error_log/home/username/tmp/logs/sample.rails.com.error.log;
    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

目前Rails 2.2之后不再内置对mysql的支持,所以必须安装mysql的 库,如下

sudo apt-get install libmysql-ruby

之后运行mongrel,如:

/usr/local/ruby/bin/mongrel_rails start -p 3001  --user username --group username -c /home/username/www/sample -e development

这样就可以了,对于一个可用的,快速配置的开发环境来说,已经足够了。以后抽空再写一下,Python的开发环境的配置。

Windows下搭建Ruby on Rails开发环境

基于一些原因,需要使用windows系统来开发Rails, 在网上搜了很久,尝试了很多次之后终于配置成功了。由于Rails的版本问题,出现了很多莫名的问题,现在写下来做一下总结。当然如果觉得麻烦其实可以使用Instant Rails,不过这东西版本太低,而且如果自己手动配置的话,对于整个框架的运行有比较好的了解。

首先确定一下前端的web 服务器使用Nginx来跑,然后Rails的话用Mongrel Service 安装windows 服务,使用Nginx来监听。

Rails方面很方便,下载Ruby安装包然后安装。 一般会自动把Ruby添加到系统Path里面去,之后看一下gem版本。 控制台上输入 gem -v, 最好升级到最高的吧,推荐到rubyforge.org 去下载,然后安装,速度会快点

接下来安装几个重要的gem, 如:rails, mongrel_service, 安装这几个gem时,会自动安装多个,具体可以输入 gem list 查看。Rail的安装比较简单,只是要注意数据库的问题,因为现在rails默认是sqlite的数据库,所以如果是mysql的话,记得把mysql里面的libmySQL.dll 文件拷贝到系统路径下。安装mongrel的时候碰到有2个地方比较麻烦,1个是gem的版本,开始下载了个比较低的,老是出错, 所以装个版本高的, 命令如下

gem install mongrel_service -v '0.3.4'

第2个问题就是平台问题,一定不要忘记下载win32的, 命令如下

gem install mongrel_service --platform x86-mswin32。

这里贴出来本人机器上安装的gem。

actionmailer (2.3.4)
actionpack (2.3.4)
activerecord (2.3.4)
activeresource (2.3.4)
activesupport (2.3.4)
cgi_multipart_eof_fix (2.5)
fxri (0.3.6)
fxruby (1.6.12 mswin32)
gem_plugin (0.2.3)
hpricot (0.6 mswin32)
log4r (1.0.5)
mongrel (1.1.5 x86-mswin32)
mongrel_cluster (1.0.5)
mongrel_service (0.3.4 i38)
mysql (2.8.1 x86-mswin32)
rack (1.0.1)
rails (2.3.4)
rake (0.8.7, 0.7.3)
rubygems-update (1.3.7, 1.)
sources (0.0.1)
win32-api (1.4.6 x86-mswin)
win32-clipboard (0.4.3)
win32-dir (0.3.2)
win32-eventlog (0.4.6)
win32-file (0.5.4)
win32-file-stat (1.2.7)
win32-process (0.5.3)win32-sapi (0.1.4)
win32-service (0.7.1 x86-m
win32-sound (0.4.1)
windows-api (0.4.0, 0.2.0)
windows-pr (1.0.9, 0.7.2)

之后的就好处理了,安装windows 服务,命令如下(-N后面是服务名称,-c是路径,之后是监听的端口和版本):

mongrel_rails service::install -N sample -c X:\sample -p 3001 -e development

删除服务

mongrel_rails service::remove -N sample

之后就是Nginx的配置,贴一下location的部分,监听的端口一致就可以了,另外注意root部分一定要是rails项目的public目录。

location / {
    proxy_pass http://127.0.0.1:3001;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

好了,重启一下Nginx服务,就可以看到Rails的页面了。

使用Nginx搭建自己的web开发环境

Nginx是一个非常好的HTTP 和反向代理服务器。 之所以说他好,搜一下关于他的资料就可以知晓,不管是稳定性,功能性,性能等方面都是非常优秀的。用惯了Apache,突然心血来潮试用一下Nginx之后,就发现速度可不是一般的快啊。而且配置也不是很难,下面来记录一下Nginx使用心得。

先来说说在Windows平台上安装,推荐一个非常好的站点,http://www.lamppr.com/, 下载 windows 版的 lamppr-win-2.6.5.zip 。这个是一个集成了的开发环境,其中包括,Nginx, memcached, mysql, php。

安装写在安装包里面,步骤及其简单, 解压, 执行setup.bat,这个是windows的bat文件,用记事本打开,发现是安装了一下memcached的win服务,因此,路径是可以改的。

之后启动服务, start.bat, 也非常简单,启动memchache, 启动mysql,启动cgi服务,最后启动nginx, 里面都是绝对路径,因此也是可以修改的。

有兴趣的朋友可以利用这些信息自己写工具,更加方便的管理。转到Ubuntu平台,安装包更加方便,启动很方便。最重要的一点就是Nginx本身不能解析php,需要启动php-cgi来解析php文件。

配置方面Nginx手册里都有,值得提的一点,对于本人的一点极好的特点,就是Nginx的配置文件方便解析,有更多乐趣。Nginx的配置文件使用花括号区分区块,使用分号断行。写脚本解析是不是很好玩呢? 呵呵

Doophp 用过的最快的php框架

前几天在http://www.lamppr.com上看到Doophp的介绍,传说是速度最快的php框架,于是google了一把,看了一下很多评论,貌似不错,来到Doophp的官网,稍微看了一下就把Framework给下载下来了,但是不知什么原因,API的包下载下来解压缩有问题,也许是操作系统原因吧。不过不碍事,马上动手搭建起来。

先介绍一下文件夹结构,解压出来3个文件夹和4个TXT文件,TXT文件中我就看了INSTALL和CHANGELOG,看一下怎么安装,更改日志的话我的目的主要是看看里面有那些内容,作者最近修改的BUG在哪些方面,这样有个粗略的印象。文件夹包括dooframework,这个是doo的框架,demo是一些样例,app主要就是工作的目录。

app这个目录下是开发者真正比较关心的(虽然同样关心dooframework这个目录

里面有已经写好的index.php(应用程序的入口,负责初始化配置,路由,数据库等),一个.htaccess文件(主要用来让url更好看,真正做到RESTful)

global文件夹(静态文件,比如css,js等)

protected文件夹(php的工作目录,里面有控制器,模型,视图,插件,配置文件,缓存等目录)

tools文件夹(这个文件夹还是比较有用,开始用的时候可以使用他生成控制器,模型,路由等等,值得一提的是模型里面支持各种关系,比较好用)。

好了,这时就可以把app下的所有文件和dooframework搬到新建的虚拟主机下,开始doophp之旅了。

Open Source的好处就是代码开源,文档全,有做Demo,因此搭建起来毫不费力,改了一下config下的common.conf.php的一些配置,默认应用就跑起来了。

列一下需要注意的地方: SITE_PATH 站点的文件路径,BASE_PATH 框架的文件路径,可以自定义错误页面,可以定memcache的服务器等。

当然还有数据库配置文件,路由文件,不过这些一开始都不需要配置。

稍微配置一下后,看到的页面是doo自带的页面,这个时候可以点击默认页提供的几个链接看看,有一个是可以到Tools目录下的,其中有工具还是很有用的,比如生成router,填写好地址,控制器和控制器方法,会直接出来一条路由语句,形如:

$route['*']['/home'] = array('HomeController', 'index');

非常方便,其实这个也可以到config目录下的routers.conf.php去查看,都有,作者的注释写的非常好,很清晰,一下就能明白怎么回事,作为用户来说该怎么做,这点也反映到框架本身,比如我在使用视图的时候,不知道有没有语法支持循环,判断,外部include模板等功能,打开框架里面的Dooview class一看,一目了然,各种支持情况都有写,而且还有示例,可见作者非常认真啊。实现的功能函数也很容易理解运作机制。粗看了一下,几个东西都已经能够实现了,比如MVC,URI Router,Rest,ORM, Template等,而且上手很快,运行速度也很快。

讲一下几个核心的地方和我觉得需要改进的东西:

  1. MVC, 实现的还是很好的,三个部分都分开,依靠URI,调用相应的Controller,然后Controller里面Load需要的Template。

  2. ORM, 这块我是觉得做的很不错的,是类似Hibernate的实现,不是Activerecord,怪不得官网上说它不是抄ROR的, :)

  3. Template,有点像CI的,如果模板在不同文件夹中,不好操作,须改框架源码。

  4. Tutorial做得不够完善,还是需要时间和人去做的,不过好在代码注释相当好,很容易理解。

  5. 有迁移脚本,没有深究,表面上看应该还在持续改进中。

  6. 社区一般,可能由于是中国人写的,所以中国社区比较活跃,其他的都一般。

  7. 数据验证,分页控件,图片上传处理,日志这些都有,不过本人没有深入,就瞄了一眼源码。

由于没有深究,也没有更深入的理解,不过,本人还是觉得这个框架可以一试,基本的一些东西都已经有了,速度也很快,中小型站点应该没有问题。虽然本人不是特别拥护php框架,甚至有时候是觉得php不应该有所谓的框架,但是在未来还是会一直关注Doophp,起码它在很大程度上增加了程序员的效率,节省很多时间和精力。祝愿这个框架继续进步。

初试PHP框架

随着Php一个接着一个新版本的release, 相信好多php开发者都走进了新的开发模式的世界,各种框架翻涌而出,针对不同需求,使用相对应的框架,使开发事半功倍,笔者也下载了几个尝试了一下,在做几个相同demo的时候,确实发现收效很大,针对不同的业务,只要模型建的好,遵守框架的规则,开发一个能用的雏形,根本就是几分钟的事情

不过回过头来看看rails, 相比之下还是略显不足,原因很简单,虽然php的脚本运行很快,单毕竟是脚本,每一个请求到执行完成的时候也就是此请求内对象生命结束的时候,比起rails, java,python这些来说太浪费了,不过也有好处。

CodeIgniter 是比较轻量级的一个php框架,比较简单,但是开发极其快速,代码方面是学习rails的,一看文件夹部署就知道,模型,控制器,视图都很简单,url路由也很简单,因此上手快,最喜欢他的Tutorial和视频教程,虽然语速有点快。对于这个框架最吸引人的就是好多针对各种业务逻辑的类库和辅助方法,而且在持续不断更新中。

Cakephp 也是学rails的,看了controller和model的源码,用了比较复杂的方法来写控制器和模型的超类,比如模型类预定义了很多方法和变量来操作模型,模型关系,数据库操作等,为子类提供了相当多的方便,不过在子类里面还是要设置点东西的,Cake的教程也还不错,不过框架本身设计良好,但是为了兼容php4,笔者觉得正是因为这个,稍稍影响了框架的效率,不过这也算是个好处。

Zendframwork太大了,稍微看了一下有点头痛,放弃了。

Yii 是新晋的东西,也是我觉得特别好的,特别符合php发展路线了。里面运用了很多php5的新特性,因此兼容性不是很好,不过这也是他的聪明之处。 里面最让我觉得心领神会的东西,就是使用了php的自省机制,要知道,只要定义了模型文件的属性,并且和数据库的字段相对应,对于数据库操作可是节省了很大一笔开销,另外使用公有的方法定义表名,表关系等模型文件需要的东西就可以很方便的实现模型间的关系,另外还可以为模型的属性设置Label, 即在视图部分的字段描述。 模型也内置了错误机制。

其实Yii最吸引我的原因是因为笔者之前都一直想做个趁手的框架来增加开发效率,后来尝试了很多框架,包括rails,Django等比较流行的,发现php并不适合做很复杂的框架,后来就用了另外一种方法来实现,就是代码生成的方法,基本可以实现几种模型关系,比如1对1,1对多和多对多, 控制器也很简单,分析url,直接去url中的内容来做控制器的action方法。

之后php5发布了很多OO方面的特性,笔者尝试将数据库操作从模型文件中分离出来,因此模型类就更加纯了,所以在进行数据库操作的时候,需要一些其他技巧来帮助模型与数据库操作层的沟通,因此笔者学习了一下Hibernate,虽然之前接触过java,也做了大约2年不到的项目,不过只接触过struts和JDBC, 因为那个时候Hibernate还没有那么红。不过学习完之后才幡然醒悟, 原来可以借助xml完成很多工作。不过之前我也使用过SAX来读取xml作为数据库对应的配置文件,不过太伤性能了,因此放弃了,才想到生成代码的方法。不过这次不一样,笔者使用xml文件来配置数据库字段以及字段属性和模型类属性的关系, 然后再根据xml来生产模型文件和控制器以及视图文件,基本上中小型应用完全可以搞定,不过前提是模型和数据库设计良好。 经历了很多曲折,笔者决定如果有机会的话可以向Java发展,因为尝试了一些java的demo,用了好多以前没玩过的新东西,虽然现在看来可以是老古董了,比如ANT,struts等,也尝试了Maven, 越来越觉得还是Java的OO纯正,J2EE的MVC才是正道。 不过对于php来说Yii已经很不错了,用起来很舒服,我想中大型的应用Yii完全可以胜任,小东西么,CI和我自己的工具都可以了搞定的。