Yii高性能PHP框架
Yii Framework是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。Yii是最有效率的PHP框架之一。Yii是创始人薛强的心血结晶,于2008年1月1日开始开发。本文为大家讲解的是YII 开启URL伪静态方法,服务器以apache和nginx为例,感兴趣的同学参考下。
想要在yii中开启url伪静态需要配置三个地方
1,apache开启mod_rewrite模块(或nginx)
2,yii中配置打开url重写功能
3,设置apache的重新规则
现在我们就分别来说一下各部分如何操作
1.开启apache的mod_rewrite模块
去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号
确保<Directory "..."></Directory>中有“AllowOverride All”
2.配置yii的url重写功能
在项目中的/protected/config/main.php中添加代码(yii的main.php中一般都有现成的,你只需要去掉注释就可以了)
// application components
'components'=>array(
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix'=>'.html',
'rules'=>array(
'lists/<cid:\d+>_<page:\d+>'=>'article/lists',
'lists/<cid:\d+>'=>'article/lists',
),
),
上列中是把list/1_2.html这种内容列表的url解析到article/lists这个控制上了,这部分也是最主要的配置不好就不能用,我们现在重点解说一下,主要配置的地方有三处
1,showScriptName 这是决定是否隐藏index.php的,隐藏的话用false
2,urlSuffix是用来配置伪静态的后缀名的,这要取决于你的网站目录规则,有的是/xx/xx.html,有的是/xx/xx/xx/
3,rules这个最主要这其实就是用来代替apache的重新规则 的,他的格式是url格式=>指向的控制器
比如我要把/about.html指向到site/about控制器上那么就是:
'about'=>'site/about',
比如我要把/article/2015/123.html指向到article/view控制器那么就是:
'article/<year:\d+>/<id:\d+>'=>'article/view',
注意这个year和id,这二个值可不是瞎写的,这二个参数要和你article/view控制中接收的变量名一样,要不然他解析不了。
3.配置apache的.htaccess文件
在与index.php文件同级目录下添加文件“.htaccess”,内容如下:Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# 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
4.配置nginx的url重写规则
location / {
root /home//webroot;
index index.html index.php index.htm;
#try_files $uri $uri/ @rewrite;
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ \.php$ {
root /home/webroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/webroot$fastcgi_script_name;
include fastcgi_params;
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
**然后多个参数的时候,需要注意匹配的rules问题(从上往下匹配)**
例子:
--------------------前三行注意顺序,否则出错,见参考网址
'oil/<mainmenu_id:\d+>/<submenu_id:\d+>'=>'article/detail',
'oil/<mainmenu_id:\d+>'=>'article/detail',
'<action:\w+>'=>'site/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
参考地址: 2、注意参数: http://xueguang668.blog.163.com/blog/static/9772212220129112426211/
3、正则表达式:
4、正文:http://www.phperz.com/article/15/0125/48271.html
本文为大家讲解的是YII 开启URL伪静态方法,服务器以apache和nginx为例,感兴趣的同学参考下。