之前在sina博客写过yii的文章,来到博客园之后,没再写过关于yii的文章,正好端午假期没啥事,就结合以前的博客,yii的官方文档,再加上最近的关于yii的收获总结一下,写个系列~~
yii是一个基于组件的高性能php框架,用于开发大型web应用。yii采用严格的oop编写,并有着完善的库引用以及全面的教程。从 mvc,dao/activerecord,widgets,caching,等级式rbac,web服务,到主题化,i18n和l10n,yii提供了今日web 2.0应用开发所需要的几乎一切功能。事实上,yii是最有效率的php框架之一。yii是一个高性能的php5的web应用程序开发框架。通过一个简单的命令行工具 yiic 可以快速创建一个web应用程序的代码框架,开发者可以在生成的代码框架基础上添加业务逻辑,以快速完成应用程序的开发。
安装yii在安装yii之前,你必须配置好你的开发环境,如一台支持php5.1.0以上版本的web服务器。yii已经在windows和linux操作系统上的 apache web服务器测试通过。它可能也会运行在其他平台上的支持php5的web服务器,互联网上公布了很多免费资源,你可能会获得一个配置好php5的web 服务器环境。在这里我们会抛开web服务器和php5的安装。yii的安装其实非常简单,实际只需要两个步骤:从 http://www.yiiframework.com/ 下载yii框架 解压下载文件到web服务器可访问的目录下。安装完成后,建议你检查一下当前服务器是否已经满足了yii的所有要求。幸运的是,这样做很容易,yii自带了一个简单的检查工具。要调用它,在你的浏览器地址栏中输入:http://yourhostname/path/to/yii/requirements/index.php,下面将显示你服务器的配置。使用检查工具,确定服务器没有安装和使用扩展或组件,但它只是给出一个建议,以确保可以确定安装。正如你看到的,下在的检查结果,并非都是 passed(通过)状态,也有部份显示warning(警告)。当然,你的配置情况可能会略有不同,因此,你的显示结果也会有所不同。其实下面的细节部份没有必要全部能通过。但部份也是必要的,根据 conclusion(结论)这个段落的内容:你的服务器配置满足了yii的最低要求。(your server configuration satisfies the minimum requirements by yii.)创建一个新的应用程序yii的安装位置是你已经知道的webroot是你的web服务器配置的根目录从你的命令行,进入到framework目录,并执行以下内容: % cd webroot/testdrive/framework % yiic webapp ../../testdrive create a web application under '/webroot/testdrive'? [yes|no] yes mkdir /webroot/testdrive mkdir /webroot/testdrive/assets mkdir /webroot/testdrive/css generate css/bg.gif generate css/form.css generate css/main.css
复制代码
你的应用已经成功创建到了/webroot/demo下。这个webapp命令的作用是创建一个全新的yii应用。它只需要指定一个参数,无论是绝对还是相对路径都会创建应用程序。它所生成的目录及文件只是应用程序的一个骨架。
testdrive/ index.php web 应用入口脚本文件 index-test.php 功能测试使用的入口脚本文件 assets/ 包含公开的资源文件 css/ 包含 css 文件 images/ 包含图片文件 themes/ 包含应用主题 protected/ 包含受保护的应用文件 yiic yiic 命令行脚本 yiic.bat windows 下的 yiic 命令行脚本 yiic.php yiic 命令行 php 脚本 commands/ 包含自定义的 'yiic' 命令 shell/ 包含自定义的 'yiic shell' 命令 components/ 包含可重用的用户组件 controller.php 所有控制器类的基础类 identity.php 用来认证的 'identity' 类 config/ 包含配置文件 console.php 控制台应用配置 main.php web 应用配置 test.php 功能测试使用的配置 controllers/ 包含控制器的类文件 sitecontroller.php 默认控制器的类文件 data/ 包含示例数据库 schema.mysql.sql 示例 mysql 数据库 schema.sqlite.sql 示例 sqlite 数据库 testdrive.db 示例 sqlite 数据库文件 extensions/ 包含第三方扩展 messages/ 包含翻译过的消息 models/ 包含模型的类文件 loginform.php 'login' 动作的表单模型 contactform.php 'contact' 动作的表单模型 runtime/ 包含临时生成的文件 tests/ 包含测试脚本 views/ 包含控制器的视图和布局文件 layouts/ 包含布局视图文件 main.php 所有视图的默认布局 column1.php 使用单列页面使用的布局 column2.php 使用双列的页面使用的布局 site/ 包含 'site' 控制器的视图文件 pages/ 包含 静态 页面 about.php about 页面的视图 contact.php 'contact' 动作的视图 error.php 'error' 动作的视图(显示外部错误) index.php 'index' 动作的视图 login.php 'login' 动作的视图 system/ 包含系统视图文件
复制代码
这时不用写一行代码,我们就可以在浏览器中访问如下 url 来看看我们第一个 yii 应用:
http://hostname/testdrive/index.php
复制代码
我们会看到的,这个应用包含三个页面:首页、联系页、登录页。首页展示一些关于应用和用户登录状态的信息,联系页显示一个联系表单以便用户填写并提交他们的咨询,登录页允许用户先通过认证然后访问已授权的内容。
配置在这个应用中,不管到那个页面url中都带有index.php,如果想把它去掉,怎么办。
1. 开启apache的mod_rewrite模块,去掉loadmodule rewrite_module modules/mod_rewrite.so前的#符号,确保中有allowoverride all。2. 在项目中的/protected/config/main.php中添加代码:'components'=>array( ... 'urlmanager'=>array( 'urlformat'=>'path', 'showscriptname'=>false,//注意false不要用引号括上 'rules'=>array( 'sites'=>'site/index', ), ), ... ),
复制代码
3.配置服务器,yii可以在apache和nginx下配置
1)apache
在apache服务器下,yii需要配置.htaccess文件。配置如下
rewriteengine on# prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.)redirectmatch 403 /\..*$# if a directory or a file exists, use it directlyrewritecond %{request_filename} !-frewritecond %{request_filename} !-d# otherwise forward it to index.phprewriterule . index.php
复制代码
2)nginx
yii可以使用nginx和php的fpm sapi。配置如下
server { set $host_path /www/mysite; access_log /www/mysite/log/access.log main; server_name mysite; root $host_path/htdocs; set $yii_bootstrap index.php; charset utf-8; location / { index index.html $yii_bootstrap; try_files $uri $uri/ /$yii_bootstrap?$args; } location ~ ^/(protected|framework|themes/\w+/views) { deny all; } #avoid processing of calls to unexisting static files by yii location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { try_files $uri =404; } # pass the php scripts to fastcgi server listening on 127.0.0.1:9000 # location ~ \.php { fastcgi_split_path_info ^(.+\.php)(.*)$; #let yii catch the calls to unexising php files set $fsn /$yii_bootstrap; if (-f $document_root$fastcgi_script_name){ set $fsn $fastcgi_script_name; } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param script_filename $document_root$fsn; #path_info and path_translated can be omitted, but rfc 3875 specifies them for cgi fastcgi_param path_info $fastcgi_path_info; fastcgi_param path_translated $document_root$fsn; } # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.) location ~ /\. { deny all; access_log off; log_not_found off; }}
复制代码
使用如上配置,你可以在php.ini中设置cgi.fix_pathinfo=0,这样可以避免许多不必要的系统的stat()调用。
基本安装和配置就到这里~~