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

$enablePrettyUrl 

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".

$enableStrictParsing

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.

$showScriptName 

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美化的更多相关文章

  1. yii2中的url美化

    在yii2中,如果想实现类似于post/1,post/update/1之类的效果,官方文档已经有明确的说明 但是如果想把所有的controller都实现,这里采用yii1的方法 'rules' =&g ...

  2. yii2 url 美化参数

    所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...

  3. yii2项目实战-路由美化以及如何正确的生成链接

    yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...

  4. 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 ...

  5. YIi url美化

    一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...

  6. Yii2开启enableprettyurl(url美化)无效

    最终显示的url格式为:http://localhost/yii2/frontend/web/site/about 在/config/main.php中 'components'=>[] 中添加 ...

  7. yii url美化 urlManager组件

    yii的官方文档对此的解释如下: urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...

  8. Yii2.0 URL美化

    1. 程序初始化注册文件,加入如下: 'urlManager' =>[ 'class' => 'yii\web\UrlManager', 'showScriptName' =>fal ...

  9. yii2.0 url美化-apache服务器

    //配置内容 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, //不启用严格 ...

随机推荐

  1. rcync 备份命令

    不写复杂的命令参数什么意思,网上一搜一大堆. 这是我用的命令: rsync -ax --progress -e "ssh -p 12345" --delete root@143.2 ...

  2. 基于Arduino、STM32进行红外遥控信号接收

    catalogue . 遥控器原理简介 . 红外遥控原理 . 常见红外遥控器红外线信号传输协议 . 遙控器的发展 . 实验过程 . 攻击面 . 基于STM32实现红外信号解码 1. 遥控器原理简介 0 ...

  3. shell命令lsof

    PREFACE linux一切皆是文件,共有7中文件类型 1.普通文件(regular file) 2.目录文件(directory file) 3.块特殊文件(block special file) ...

  4. JVM垃圾回收算法

  5. u盘安装Fedora23

    -2.计划用ultraiso安装 fedora 23 ,然后从windows平台转到Linux平台上. -1.概念理解 硬盘模式:achi(sata) ide (ata) 分区格式:主引导记录(mbr ...

  6. C C++ 语法

    非常酷的网站: http://yige.org/cpp/defined_data_types.php 在Linux下有一个目录/proc/$(pid),这个目录保存了进程号为pid的进程运行时的所有信 ...

  7. 前端项目构建工具---Grunt

    什么是Grunt? grunt是javascript项目构建工具,在grunt流行之前,前端项目的构建打包大多数使用ant.(ant具体使用 可以google),但ant对于前端而言,存在不友好,执行 ...

  8. Bash 中的 _ 是不是环境变量

    首先,我们想到的会是 export(等价于 declare -x)命令: $ export | grep 'declare -x _=' 没有找到,那么结论就是 _ 不是环境变量?当然没那么简单,否则 ...

  9. 大熊君大话NodeJS之------基于Connect中间件的小应用(Bigbear记事本应用之第一篇)

    一,开篇分析 大家好哦,大熊君又来了,昨天因为有点个人的事没有写博客,今天又出来了一篇,这篇主要是写一个记事本的小应用,前面的文章, 我也介绍过“Connect”中间件的使用以及“Mongodb”的用 ...

  10. JavaScript学习链接

    Js中this的用法:http://www.cnblogs.com/RitaRichard/archive/2011/10/14/2212161.html JavaScript\ActionScrip ...