一、Yii Url美化,配置urlManager组件

'urlManager' => [
'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index.php?r=
'showScriptName' => false, // 如果设置为 true,会显示 index.php
'suffix' => '.html', // 实现伪静态
'rules' => [ // 在 rules 中设置自定义规则
'<controller:\w+>/<id:\d+>' => '<controller>/detail',
'<controller:\w+>/<id:\d+>/<action:(create|update|delete)>' => '<controller>/<action>', // 在控制器和动作之间是 id 的值
'<controller:(post|comment)>s' => '<controller>/index', // 右边post和comment index 的动作 都可以用左边控制器ID加上 s 来代替
'posts' => 'post/index', // 如果访问 post/index 显示为 posts.html
],
],
'<controller:\w+>/'<id:\d+> => '<controller>/detail' 详解:
当url出现由若干字符 + / + 若干个数字来组成字符串的时候,urlManager 就会来判断这个字符串是否匹配规则左边的正则表达式,如果能匹配的话,这个字符串就会被转换成为规则右边的这种样式,在 controller后面跟上 /detail这个字符串,
然后把规则左边的 id 已经若干的数字以参数的形式跟在动作的后面。 转换详情:
例如:www.example.com/post/42.html 在规则 suffix => .html的作用下转换为:
    www.example.com/post/42    在规则'<controller:\w+>/'<id:\d+> => '<controller>/detail'的作用下转换为:
    www.example.com/post/detail?id=42 在规则 'enablePrettyUrl' => true的作用下转换为:
    www.example.com/index.php?r=post/detail?id=42 

最后这个 url 就是完整路径

二、createUrl()方法
1、UrlManager 组件的createUrl方法可以创建各种类型的url链接
2、可以把路由和要传递的参数作为 createUrl 方法的参数进行创建
3、能够自动转换为符合URL美化规则的链接
使用示例:

// url格式为:index.php?r=site%2Findex&param1=value1&param2=value2
Yii::$app->urlManager->createUrl(['site/index','param1'=>'value1','param2'=>'value2']); // url格式为:index.php?r=site%2Findex&param1=value1#name
Yii::$app->urlManager->createUrl(['site/index','param1'=>'value1','#'=>'name']);
路由 参数 锚点

记住,代码中,一定要注意尽量不要使用硬编码

三、url助手类
1、yii\helpers\Url::to()来创建各种类型的url链接
2、可以把路由和要传递的参数作为url助手类方法的参数来进行创建

// index.php?r=post%2Findex
echo Url::to(['post/index']); // index.php?r=post%2Fview&id=100
echo Url::to(['post/view','id'=>100]); // index.php?r=post%2Fview&id=100#content
echo Url::to(['post/view','id'=>100,'#'=>'content']); // index.php?r=post%2Findex
echo Url::to(['post/index'],true); // 创建一个硬链接 // https//:www.example.com/index.php?r=post%2Findex
echo Url::to(['post/index'],'https'); // 创建含有https的url

四、虚拟主机URL美化
在项目根目录新建一个 .htacces 的文件
将重写规则写入 .htacces 文件
例如写入下面这一段:

# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php # use index.php as index file
DirectoryIndex index.php # ...other settings...
# Apache 2.4
Require all granted ## Apache 2.2
# Order allow,deny
# Allow from all

httpd.conf需要改为如下:

<VirtualHost *:80>
ServerName frontend.test  // 设置虚拟主机名
DocumentRoot "/path/to/yii-application/frontend/web/" // 虚拟主机的web根目录 <Directory "/path/to/yii-application/frontend/web/">
AllowOverride All
//允许 .htacces 文件覆盖重写规则
</Directory>
</VirtualHost>
这种情况是在 虚拟主机不许给我们修改的时候使用,但是Apache服务的配置必须能被覆盖才行。

YIi url美化的更多相关文章

  1. yii url美化 urlManager组件

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

  2. Yii地址美化(nginx环境)

    通过urlmanager实现yii地址美化,需配合服务器中的rewrite配置     1.在'components'中加入    'urlManager'=>array(        'ur ...

  3. Yii2的urlManager URL美化

    Yii1.*与Yii2中配置路由规则rules是几乎是一样的,但还是有细微的差别. 在Yii1.*中开启path路由规则直接使用 'urlFormat' => 'path', 但在Yii2中已经 ...

  4. yii2 url 美化参数

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

  5. Yii URL

    参考文章: http://blog.csdn.net/iefreer/article/details/21325371 以http://localhost/basic/web/index.php?r= ...

  6. Yii url createUrl redirect相关

    一篇文章: 在yii中明明白白生成网址: 在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,act ...

  7. yii框架美化访问路径,去掉index.php/?r=部分

    一.找到配置文件(ps:advance高级模板) 在工程目录-> backend目录 或 frontend目录 -> config目录 -> main.php文件 -> 在 r ...

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

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

  9. Yii2.0 URL美化

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

随机推荐

  1. python中深拷贝与浅拷贝

    # 1.浅拷贝(复制东西)a = [11,22,33] # 实际上是浅拷贝# 没有把这个变量的值赋进去,而是把另一个变量的地址拿过去了,就叫浅拷贝.b = a # print(id(a))# prin ...

  2. 谷歌浏览器Software Reporter Tool长时间占用CPU解决办法

    什么是Software Reporter Tool Software Reporter Tool是一个Chrome清理工具,用于清理谷歌浏览器中不必要或恶意的扩展,应用程序,劫持开始页面等等.当你安装 ...

  3. Java 骚操作--生成二维码

    https://www.cnblogs.com/lsy131479/p/8808172.html

  4. es6 箭头函数【箭头表达式】

    箭头函数,通过 => 语法实现的函数简写形式,C#/JAVA8/CoffeeScript 中都有类似语法.与函数不同,箭头函数与其执行下文环境共享同一个 this.如果一个箭头函数出现在一个函数 ...

  5. Appearance-and-Relation Networks for Video Classification论文笔记 (ARTnet)

    ARTnet: caffe实现:代码 1 Motivation:How to model appearance and relation (motion) 主要工作是在3D卷积的基础上,提升了acti ...

  6. Loadrunner和JMeter并发对比

    今天在项目中测试发现,其实LR才是实际意义上的并发测试,JMeter不算并发 记录用户登录日志: LR脚本: 1.登录操作放在init初始化中,用5个虚拟用户并发测试:

  7. nginx配置2

    第四节 nginx 配置文件 1 keepalive_timeout   65;  设定保存长久连接时长 0代表禁止, 若不设置默认是75s 2keepalive_requests   nu;  在一 ...

  8. Python学习(二十五)—— Python连接MySql数据库

    转载自http://www.cnblogs.com/liwenzhou/p/8032238.html 一.Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MyS ...

  9. Codeforces Round #517 体验记

    原文链接 https://www.cnblogs.com/zhouzhendong/p/CF1071.html 赛前: 呀,这个 Round # 必须打啊. 于是临时改变注意决定打这一场.用小号打. ...

  10. java的IO流的一些测试

    public class Demo02 { public static void main(String[] args) { //获取当前毫秒值,用于测试 long l = System.curren ...