zoukankan      html  css  js  c++  java
  • YIi url美化

    一、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、yiihelpersUrl::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服务的配置必须能被覆盖才行。


  • 相关阅读:
    忘记秘密利用python模拟登录暴力破解秘密
    ubuntu16.04 install qtcreator
    ubuntu16.04 pip install scrapy 报错处理
    Ubuntu18.04 和ubuntu16.04 apt源更新
    Ubuntu16.04主题美化
    ubuntu16.04上vue环境搭建
    基于fastadmin快速搭建后台管理
    python生成linux命令行工具
    nvidia驱动自动更新版本后问题解决 -- failed to initialize nvml: driver/library version mismatch
    学会使用Google搜索
  • 原文地址:https://www.cnblogs.com/chrdai/p/8619758.html
Copyright © 2011-2022 走看看