Yii2的urlManager URL美化
Yii1.*与Yii2中配置路由规则rules是几乎是一样的,但还是有细微的差别。
在Yii1.*中开启path路由规则直接使用
'urlFormat' => 'path',
但在Yii2中已经没有urlFormat 对象方法,在Yii2取而代之的是
'enablePrettyUrl'=>TRUE,
一个典型的Yii1.* urlManager配置:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false, //隐藏index.php
'urlSuffix' => '.html', //后缀
'rules' => array(
'news/detail-<id:.*>' => 'news/detail', //http://cms.com/news/detail-27d24c26.0486c.0aea8.a3d2803b.0000111.03.html id==>[id] => 27d24c26.0486c.0aea8.a3d2803b.0000111.03
'news/<id:.*>-<category:\d+>' => 'news', //http://cms.com/news/02f5bc8f-04600-0b477-c6bc82ab-0000111-03-1.html ==== $this->createUrl('news/', array('id' => $value['id'],'category'=>1));
'singlePage/<id:.*>' => 'singlePage',
'contact' => 'about/contact',
'addOrder' => 'Online/addOrder',
/**
* $this->createUrl('news/index',array('userid'=>123,'title'=>3434,'nid'=>'sdfsdfsdf')) index.php/new/index?userid=123&title=3434&nid=sdfsdfsdfsd
* http://cms.com/news/123/3434/sdfsdfsdf-index.html
*/
'news/<id:\d+>/<title:.*?>/<nid:.*?>-index' => 'news/index',
'refresh/' => 'index/Refresh',
'index.jsp/' => 'index/',
'index.aspx/' => 'index/',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
那Yii2如何配置呢?
首先我们的URL地址是这样的
http://new.com/index.php?r=auth%2Fmenulink&post=2
我们要让地址改为path模式:
http://new.com/auth/menulink/post/2
1.在Nginx中开启rewrite
server {
listen 80;
server_name new.com ;
location / {
root F:/www/new/web;
index index.html index.htm index.php;
#autoindex on;
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
location ~ \.php$ {
root F:/www/new/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2.config中web.php 配置
'urlManager' => [
'enablePrettyUrl' => true, //美化url==ture
'enableStrictParsing' => false, //不启用严格解析
'showScriptName' => false, //隐藏index.php
'rules' => [
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'/m-api/' => '/meanpan-api/index', // 默认index
'/mp-api/<action>' => '/meanpan-api/<action>', // /mp-api/*==>/meanpan-api/*
],
]
参数说明:
Yii官网说明:http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
Whether to enable pretty URLs. Instead of putting all parameters in the query string part of a URL, pretty URLs allow using path info to represent some of the parameters and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of "/index.php?r=news/view&id=100".
Whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route. This property is used only when $enablePrettyUrl is true.
Whether to show entry script name in the constructed URL. Defaults to true. This property is used only if $enablePrettyUrl is true.
现在访问URL就成为path模式了。
启用开发模式DEBUG
if (YII_ENV_DEV) {
// 启用开发模式DEBUG
$config['bootstrap'][] = 'debug';
$config['modules']['debug']['class'] = 'yii\debug\Module';
$config['modules']['debug']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
$config['modules']['debug']['historySize'] =500; # 修改debug记录条数 $config['bootstrap'][] = 'gii';
$config['modules']['gii'] ['class']= 'yii\gii\Module';
$config['modules']['gii']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
}
Yii2的urlManager URL美化的更多相关文章
- yii2中的url美化
在yii2中,如果想实现类似于post/1,post/update/1之类的效果,官方文档已经有明确的说明 但是如果想把所有的controller都实现,这里采用yii1的方法 'rules' =&g ...
- yii2 url 美化参数
所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...
- yii2项目实战-路由美化以及如何正确的生成链接
yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...
- Yii2实现即可以美化路由访问又可以原始路由访问
1. 本地环境 nginx version: nginx/1.11.1 PHP 7.1.0-dev (cli) mysql Ver 14.14 Distrib 5.7.22, for Linux (x ...
- YIi url美化
一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...
- Yii2开启enableprettyurl(url美化)无效
最终显示的url格式为:http://localhost/yii2/frontend/web/site/about 在/config/main.php中 'components'=>[] 中添加 ...
- yii url美化 urlManager组件
yii的官方文档对此的解释如下: urlSuffix 此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...
- Yii2.0 URL美化
1. 程序初始化注册文件,加入如下: 'urlManager' =>[ 'class' => 'yii\web\UrlManager', 'showScriptName' =>fal ...
- yii2.0 url美化-apache服务器
//配置内容 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, //不启用严格 ...
随机推荐
- Ubuntu下Apache的安装
Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5),参见<Ubuntu下快速安装LAMP server>一文. 也可以手动安装Apache.本文介绍如何手 ...
- php创建多级目录的两种方法
1.使用递归的思想 function mkdirs_2($path){ if(!is_dir($path)){ mkdirs_2(dirname($path)); if(!mkdir($path, 0 ...
- 关于@property()的那些属性及ARC简介
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- java异常类
package shb.java.exception; /** * 测试异常类 * @Package:shb.java.exception * @Description: * @author shao ...
- 选择屏幕(Selection Screen)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- LA 3904
一道DP题: 一共有三种砖,1*2,2*1,2*2,然后要你铺满整个n*2的地板,求不重复的铺法数: 方法: 首先计算了不考虑对称的情况,然后计算只考虑对称的情况: 所以结果就是(不考虑对称数+只考虑 ...
- Python3 randrange() 函数
描述 randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1. 语法 以下是 randrange() 方法的语法: import random random.randrange ...
- VMware对虚拟机快照进行克隆
1.在关机状态下做一个快照 2.把快照管理器打开 3.右键快照,选择“克隆此快照” 4.选择要克隆的快照 5.选择克隆的方式 6.设置名称及保存的位置 注:虚拟机的快照是开机状态,不能对快照进行克隆
- 使用 Windows PowerShell 实现 Web UI 自动化 (转)
转自 https://msdn.microsoft.com/zh-cn/magazine/cc337896.aspx
- 日期在Linux与Windows下的区别
最近遇到了这个问题,就是相同的代码在Windows与Linux下的日期转换不一致. 原因:时区问题,主要是操作系统与JVM中的时区不同导致的 在网上查了很多处理的方法:最后总结出一条简单粗暴的方法:原 ...