欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

yii2.0 URL美化

程序员文章站 2023-03-31 16:35:42
基础版 web.php,高级本 main.php 在 components下添加配置 默认使用 yii\web\UrlRule 类。如果是使用api模式的话需指定类 'urlManager' => [ 'showScriptName' => true,//是否显示入口文件index.php 'ena ......
基础版 web.php,高级本 main.php
components下添加配置 默认使用 yii\web\urlrule 类。如果是使用api模式的话需指定类
 
'urlmanager' => [
'showscriptname' => true,//是否显示入口文件index.php
'enableprettyurl' => true,//是否美化url
'enablestrictparsing' => true,//严格模式 开启后必须添加rules规则
'suffix' => '.html', // 后缀
'rules' => [
'<module:\w+-?\w+>/<controller:\w+-?\w+>/<action:\w+-?\w+>' => '<module>/<controller>/<action>',
'<controller:\w+-?\w+>/<action:\w+-?\w+>' => '<controller>/<action>',
],
],
 
规则里表示 有模块和没有模块的对应规则
\w+-?\w+ 其实就是一个正则匹配。匹配 daa2-fdfa 这种。假如请求的格式匹配不到的话将会报错
 
隐藏入口文件时,配置nginx 首先得开启nginx pathinfo模式:
location ~ \.php { #去掉$
root e:/phpstudy/www/tp/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param path_info $fastcgi_path_info;
fastcgi_param script_filename $document_root$fastcgi_script_name;
include fastcgi_params;
}
请求指定路径时重写url 下面是项目路径
location /server/admin/web/ {
if (!-e $request_filename) {
rewrite ^/server/admin/web/(.*)$ /server/admin/web/index.php/$1;
}
}