问题背景:
在router.php路由配置文件设置了二级域名和路由设置
use think\Route; Route::domain(‘www‘,‘index‘); Route::domain(‘admin‘,‘admin‘); Route::domain(‘m‘,‘m‘); Route::rule(‘show/:id‘,‘index/Article/item‘); Route::rule(‘about_us‘,‘index/index/about_us‘);
遇到的问题:
访问 www.t.com/about_us 会访问 index/index/about_us 即index模块index控制器的about_us方法;
但是 访问 admin.t.com/about_us 也会访问 index/index/about_us ;这显然是我们不想看到的
解决思路:
能不能在Route的rule()中做一个判断?如果当前域名是绑定了admin模块的,但是设置的路由地址(即:index/index/about_us) 的模块不是admin模块,那么就忽略该路由设置
修改源码:
/** * 注册路由规则 * @access public * @param string|array $rule 路由规则 * @param string $route 路由地址 * @param string $type 请求类型 * @param array $option 路由参数 * @param array $pattern 变量规则 * @return void */ public static function rule($rule, $route = ‘‘, $type = ‘*‘, $option = [], $pattern = []) { $r_data = explode(‘/‘,$route); $host_data = explode(‘.‘,$_SERVER [‘HTTP_HOST‘]); if(count($r_data) == 3 && count($host_data) == 3){ $erji_host = $host_data[0]; if(isset(self::$rules[‘domain‘][$erji_host])){ if(self::$rules[‘domain‘][$erji_host][‘[bind]‘][0] != $r_data[0]){ return false; } } } $group = self::getGroup(‘name‘); if (!is_null($group)) { // 路由分组 $option = array_merge(self::getGroup(‘option‘), $option); $pattern = array_merge(self::getGroup(‘pattern‘), $pattern); } $type = strtolower($type); if (strpos($type, ‘|‘)) { $option[‘method‘] = $type; $type = ‘*‘; } if (is_array($rule) && empty($route)) { foreach ($rule as $key => $val) { if (is_numeric($key)) { $key = array_shift($val); } if (is_array($val)) { $route = $val[0]; $option1 = array_merge($option, $val[1]); $pattern1 = array_merge($pattern, isset($val[2]) ? $val[2] : []); } else { $option1 = null; $pattern1 = null; $route = $val; } self::setRule($key, $route, $type, !is_null($option1) ? $option1 : $option, !is_null($pattern1) ? $pattern1 : $pattern, $group); } } else { self::setRule($rule, $route, $type, $option, $pattern, $group); } }
原文地址:https://www.cnblogs.com/cl94/p/11745817.html
时间: 2024-10-10 21:11:38