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, //不启用严格 ...
随机推荐
- php cli配置文件问题
引言 今天在教别人使用protobuf的时候,无意中发现了一个php cli模式下的诡异问题,费了老半天的找到解决方法了,这里拿出来分享下. 问题描述 我们这边最先引入了protobuf协议,使用的是 ...
- TAC Beta版本 冲冲冲!!!
一.Beta版本冲刺博客目录: 第一天 第二天 第三天 第四天 第五天 第六天 第七天 二.Beta版本需要改进完善的功能: service层传入参数的判断与提示以及各函数内的相应提示 界面改进.优化 ...
- Windows中创建桌面快捷方式
Windows中创建桌面快捷方式 -------------- -------------- -------------- --------------
- 第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)
6.3 突破传统,4k大屏的沉浸式体验 前言 能够在 4K 的页面上表演,对设计师和前端开发来说,即是机会也是挑战,我们可以有更大的空间设计宏观的场景,炫酷的转场,让观众感受影院式视觉体验,但是,又必 ...
- C#成员函数直接调用和反射+委托的性能比较
using System; using System.Reflection; using System.Diagnostics; namespace Refl { class Test { publi ...
- Unity unsafe
http://answers.unity3d.com/questions/23307/is-it-impossible-to-use-the-code-with-keyword-unsa.html 可 ...
- live555库中的testH264VideoStreamer实例
1.h264文件的推送 testH264VideoStreamer.cpp文件的开头就定义了 char const* inputFileName = "test.264"; 后面接 ...
- hibernate的集中持久化方法的区别
一.预备知识 在所有之前,说明一下,对于hibernate,它的对象有三种状态,transient.persistent.detached 下边是常见的翻译办法: transient:瞬态或者自由态 ...
- MySQL 调优/优化的 100 个建议
MySQL 调优/优化的 100 个建议 MySQL是一个强大的开源数据库.随着MySQL上的应用越来越多,MySQL逐渐遇到了瓶颈.这里提供 101 条优化 MySQL 的建议.有些技巧适合特定 ...
- Python调用服务接口
#! /usr/bin/env python # coding=utf-8 ############################################################## ...