之前在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應用程序的代碼框架,開發者可以在生成的代碼框架基礎上添加業務邏輯,以快速完成應用程序的開發。
% 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前的"#"符號,確保<Directory "..."></Directory>中有"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 directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . 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()調用。
基本安裝和配置就到這裡~~