QQ扫一扫联系
正常链接为 http://cms.com/Message
怎样访问 http://cms.com/message 时也可以正常访问页面。
PHP版本8.1、系统版本 cms laravel9
1、在app\Http\ 中添加文件 CaseInsensitiveUriValidator.php
app\Http\CaseInsensitiveUriValidator.php 文件代码内容:
<?php namespace App\Http\Middleware; use Illuminate\Http\Request; use Illuminate\Routing\Route; use Illuminate\Routing\Matching\ValidatorInterface; // URL不区分大小写 class CaseInsensitiveUriValidator implements ValidatorInterface { public function matches(Route $route, Request $request) { $path = $request->path() == '/' ? '/' : '/' . $request->path(); return preg_match(preg_replace('/$/', 'i', $route->getCompiled()->getRegex()), rawurldecode($path)); } }
2、 在 module\Cms\Web\routes.php 文件顶部添加以下内容:
<?php // URL不区分大小写 use Illuminate\Routing\Route as IlluminateRoute; use App\Http\Middleware\CaseInsensitiveUriValidator; use Illuminate\Routing\Matching\UriValidator; $validators = IlluminateRoute::getValidators(); $validators[] = new CaseInsensitiveUriValidator; IlluminateRoute::$validators = array_filter($validators, function($validators) { return get_class($validators) != UriValidator::class; });
3、在module/Cms/Util/CmsCatUtil.php上修改 getByUrl方法。
public static function getByUrl($url) { $url = strtolower($url); foreach (self::all() as $item) { if (strtolower($item['url']) == $url) { return $item; } } return null; }
// 这样URL可以不区分大小写了。