小丑路人 2017-10-17 15:00:19 315次浏览 0条评论 0 0 0

第一步:修改系统跳转的函数

路径: \vendor\yiisoft\yii2\web\UrlManager.php

public function createUrl($params){
    $params = (array) $params;
    $anchor = isset($params['#']) ? '#' . $params['#'] : '';
    unset($params['#'], $params[$this->routeParam]);

    /**
     * 用于检测是否跳转其他模块!是不同的模块文件夹而不是模型名称!
     *  例如:模块文件夹:admin后台,index前台,user用户中心
     * 三层,那么跳转 模块$params[0],的$params[1]控制器,的$params[2]方法
     *
     *
     * is used to detect whether to jump other modules! It's a different module folder, not a model name!
     * such as module folder: admin background, index front desk, user user center
     * the three layer, then the jump module $params[0], the $params[1] controller, the $params[2] method
     */
    $paramsAry =  explode('/',$params[0]);
    $route = trim($params[0], '/');
    unset($params[0]);

    $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

    if ($this->enablePrettyUrl) {
        $cacheKey = $route . '?';
        foreach ($params as $key => $value) {
            if ($value !== null) {
                $cacheKey .= $key . '&';
            }
        }
        $url = $this->getUrlFromCache($cacheKey, $route, $params);

        if ($url === false) {
            $cacheable = true;
            foreach ($this->rules as $rule) {
                /* @var $rule UrlRule */
                if (!empty($rule->defaults) && $rule->mode !== UrlRule::PARSING_ONLY) {
                    // if there is a rule with default values involved, the matching result may not be cached
                    $cacheable = false;
                }
                if (($url = $rule->createUrl($this, $route, $params)) !== false) {
                    if ($cacheable) {
                        $this->setRuleToCache($cacheKey, $rule);
                    }
                    break;
                }
            }
        }

        if ($url !== false) {
            if(count($paramsAry) >=3){//三层参数,进行模块之间的跳转
                $baseUrl = str_replace(\Yii::$app->controller->modelName,$paramsAry[0],$baseUrl);
                $url = str_replace($paramsAry[0].'/','',$url);
            }
            if (strpos($url, '://') !== false) {
                if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
                    return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
                } else {
                    return $url . $baseUrl . $anchor;
                }
            } else {
                return "$baseUrl/{$url}{$anchor}";
            }
        }

        if ($this->suffix !== null) {
            $route .= $this->suffix;
        }
        if (!empty($params) && ($query = http_build_query($params)) !== '') {
            $route .= '?' . $query;
        }
        if(count($paramsAry) >=3){//三层参数,进行模块之间的跳转
            $baseUrl = str_replace(\Yii::$app->controller->modelName,$paramsAry[0],$baseUrl);
            $route = str_replace($paramsAry[0].'/','',$route);
        }
        return "$baseUrl/{$route}{$anchor}";
    } else {
        if(count($paramsAry) >=3){//三层参数,进行模块之间的跳转
            $baseUrl = str_replace(\Yii::$app->controller->modelName,$paramsAry[0],$baseUrl);
            $route = str_replace($paramsAry[0].'/','',$route);
        }
        $url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
        if (!empty($params) && ($query = http_build_query($params)) !== '') {
            $url .= '&' . $query;
        }

        return $url . $anchor;
    }
}

第二步:定义模块名称变量

路径:\vendor\yiisoft\yii2\base\Controller.php
定义变量(和$module放在一块吧!随你自己,这个无所谓的)

/**
 * @var $modelName
 * 模块名 该项目的文件目录名称,主要用于模块之间的跳转!
 * Module name, the name of the file directory of the project, mainly used for the jump between modules!
 */
public $modelName;

第三步:修改系统函数

路径:\vendor\yiisoft\yii2\base\Controller.php

public function __construct($id, $module, $config = []){
    $this->id = $id;
    $this->module = $module;
    /**
     * \Yii::getAlias('@rootUrl')  你网站的根目录
     */
    $file_name   =   explode('/',trim($_SERVER['SCRIPT_NAME'],\Yii::getAlias('@rootUrl').'/'));
    $this->modelName = $file_name[0];
    unset($file_name);
    parent::__construct($config);
}

设置完毕!
调用:
\yii\helpers\Url::toRoute('模块名称/控制器名/方法名');

如果不写模块名称的话,就是系统默认的跳转方法!

ajax checkboxlist dropdownlist yii
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册