关于YII2下方的模块之间跳转的核心代码扩展 [ 技术分享 ]
第一步:修改系统跳转的函数
路径: \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('模块名称/控制器名/方法名');
如果不写模块名称的话,就是系统默认的跳转方法!
共 3 条回复
-
实在是不好意思,这个有一定的BUG对于redirect方法。
一步完成:
修改系统跳转的函数
路径: \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 && @explode('/',$baseUrl)[1]){//三层参数,进行模块之间的跳转 $baseUrl = str_replace(@explode('/',$baseUrl)[1],@$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 && @explode('/',$baseUrl)[1]){//三层参数,进行模块之间的跳转 $baseUrl = str_replace(@explode('/',$baseUrl)[1],@$paramsAry[0],$baseUrl); $route = str_replace($paramsAry[0].'/','',$route); } return "$baseUrl/{$route}{$anchor}"; } else { if(count($paramsAry) >=3 && @explode('/',$baseUrl)[1]){//三层参数,进行模块之间的跳转 $baseUrl = str_replace(@explode('/',$baseUrl)[1],@$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; } }
函数替换就好了。支持redirect跳转和\yii\helpers\Url::toRoute。
调用:
\yii\helpers\Url::toRoute('模块名称/控制器名/方法名'); $this->redirect('模块名称/控制器名/方法名'); 如果不写模块名称的话,就是系统默认的方法!
YII2接触不久,不知道还有什么URL获取的,如果其他有BUG,请及时反馈,谢谢,大家一起进步哈!****
-
第二步和第三部,单独分离出来,这里就不需要了。
关于YII2获取模块名称的设置
第一步:定义模块名称变量
\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::$app->controller->modelName 如果不写模块名称的话,就是系统默认的方法!
小丑路人
注册时间:2016-12-11
最后登录:2023-07-26
在线时长:7小时32分
最后登录:2023-07-26
在线时长:7小时32分
- 粉丝1
- 金钱510
- 威望20
- 积分780