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. powerdesigner-从excel导入table模型

    近在使用pd过程中,遇到一个问题,就是类的字段,方法,类型在excel中整理好了,想导入到pd直接生成类图.网上有很多生成实体表的方法,于是自己模仿写了一个生成类图的,在pd中的工具--扩展--脚本, ...

  2. C#-WebForm-ASP开发练习:从数据库中动态添加信息

    传统的ASP开发方式,是C#代码和HTML代码混合在一起,ASP与ASP.NET不是一个东西. <%  %>  -  可以扩起来一段范围,这一段范围之内只能允许编写C#代码 <%= ...

  3. 【USACO 2.3】Money Systems(dp)

    v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...

  4. Thinking in java学习笔记之final

  5. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  6. MQTT 消息 发布 订阅

    当连接向一个mqtt服务器时,clientId必须是唯一的.设置一样,导致client.setCallback总是走到 connectionLost回调.报connection reset.调查一天才 ...

  7. Html中行内样式的设置

    Html中行内样式的设置.. <html> <head> <title>显示的页面选项卡标题</title> <style type=" ...

  8. HTTP超文本传输协议-HTTP/1.1中文版

    摘要 超文本传输协议(HTTP)是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议.它是一种通用的,不分状态(stateless)的协议,除了诸如名称服务和分布对象管理系统之类的超文本用途外 ...

  9. 6个重要的.NET概念:栈,堆,值类型,引用类型,装箱,拆箱

    引言 本篇文章主要介绍.NET中6个重要的概念:栈,堆,值类型,引用类型,装箱,拆箱.文章开始介绍当你声明一个变量时,编译器内部发生了什么,然后介绍两个重要的概念:栈和堆:最后介绍值类型和引用类型,并 ...

  10. nginx虚拟主机配置小结

    nginx的安装在lnmp环境搭建中已经介绍过了,配置文件在安装目录下的conf子目录下,主要主要分成四部分:main(全局设置).server(主机设置).upstream(负载均衡服务器设置).l ...