小丑路人
- 小丑路人 发表了说说加油ヾ(◍°∇°◍)ノ゙
- 小丑路人 2017-10-24 已签到连续签到2天,获得了10个金钱
- 小丑路人 2017-10-23 已签到连续签到1天,获得了5个金钱
- 小丑路人 2017-10-21 已签到连续签到5天,获得了20个金钱
- 小丑路人 回复了话题 关于YII2下方的模块之间跳转的核心代码扩展
这里的模块名称是前后端或者个人中心文件夹的名称,而不是模型的名称!请看清楚,因为我之前需要使用,搜出来都是model模型名称。
- 小丑路人 回复了话题 关于YII2下方的模块之间跳转的核心代码扩展
第二步和第三部,单独分离出来,这里就不需要了。
关于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 如果不写模块名称的话,就是系统默认的方法!
- 小丑路人 回复了话题 关于YII2下方的模块之间跳转的核心代码扩展
实在是不好意思,这个有一定的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,请及时反馈,谢谢,大家一起进步哈!****
- 小丑路人 2017-10-20 已签到连续签到4天,获得了20个金钱
- 小丑路人 2017-10-19 已签到连续签到3天,获得了15个金钱