Yii2实现即可以美化路由访问又可以原始路由访问

1. 本地环境

nginx version: nginx/1.11.1
PHP 7.1.0-dev (cli)
mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper

2. 分析

  • 原始路由形式:index.php?r=site/index
  • 美化后路由形式:index.php/site/index
  • 美化后需要配置
//config/main.php
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],
//这个是yii2提供的路由美化配置

3. 美化原理

  • yii2实现路由美化是通过urlManager组件实现,源码如下:
public function parseRequest($request)
{
    if ($this->enablePrettyUrl) {
        /* @var $rule UrlRule */
        foreach ($this->rules as $rule) {
            $result = $rule->parseRequest($this, $request);
            if (YII_DEBUG) {
                Yii::debug([
                    'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
                    'match' => $result !== false,
                    'parent' => null,
                ], __METHOD__);
            }
            if ($result !== false) {
                return $result;
            }
        }

        if ($this->enableStrictParsing) {
            return false;
        }

        Yii::debug('No matching URL rules. Using default URL parsing logic.', __METHOD__);

        $suffix = (string) $this->suffix;
        $pathInfo = $request->getPathInfo();
        $normalized = false;
        if ($this->normalizer !== false) {
            $pathInfo = $this->normalizer->normalizePathInfo($pathInfo, $suffix, $normalized);
        }
        if ($suffix !== '' && $pathInfo !== '') {
            $n = strlen($this->suffix);
            if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) {
                $pathInfo = substr($pathInfo, 0, -$n);
                if ($pathInfo === '') {
                    // suffix alone is not allowed
                    return false;
                }
            } else {
                // suffix doesn't match
                return false;
            }
        }

        if ($normalized) {
            // pathInfo was changed by normalizer - we need also normalize route
            return $this->normalizer->normalizeRoute([$pathInfo, []]);
        }

        return [$pathInfo, []];
    }

    Yii::debug('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
    $route = $request->getQueryParam($this->routeParam, '');
    if (is_array($route)) {
        $route = '';
    }

    return [(string) $route, []];
}
  • 以上代码显示如果开启了路由美化原始路由就不可以用了,我们如果想实现美化路由与原始路由都可以访问,就需要重写urlManager组件类中的这个方法,在componets文件夹新建类urlManaer并重写上面这个方法代码如下:
<?php
/**
 * Created by PhpStorm.
 * User: gift
 * Date: 2018/12/29
 * Time: 14:50
 */

namespace common\components;


class urlManager extends \yii\web\UrlManager
{
    /**
     * 解析请求路由
     * 同时支持 URL 原始和美化的请求解析
     *
     * @param yii\web\Request $request
     * @return array|bool
     */
    public function parseRequest($request)
    {
        $route = trim($request->get($this->routeParam));

        $enablePrettyUrl = $this->enablePrettyUrl;

        if( $route != '' ) $this->enablePrettyUrl = false;
        $result = parent::parseRequest($request);
        if( $route != '' ) $this->enablePrettyUrl = $enablePrettyUrl;

        return $result;
    }
}
  • 修改使用urlManager类
'urlManager' => [
    'class' => 'common\components\urlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],
  • 至此就实现了原始路由与美化路由都可以访问了,当然还可以隐藏index.php入口脚本
posted @ 2019-01-02 09:44  Mr-Qiang  阅读(1737)  评论(0编辑  收藏  举报