如何使Laravel中的路由不区分大小写? [英] How to make routes in Laravel case insensitive?

查看:470
本文介绍了如何使Laravel中的路由不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel中有一个项目,该项目中有很多路线.

I have a project in laravel and there are many routes in that project.

但是我只是发现路由都是区分大小写的,这意味着 /advertiser/reports /advertiser/Reports 不同.

But i just discovered that the routes are all case sensitive, means /advertiser/reports is different than /advertiser/Reports .

所以我想要的是两个路由都应重定向到同一视图.目前 /advertiser/Reports 给出RouteNotFound异常.

So what i want is both the routes should redirect to same view. Currently /advertiser/Reports gives RouteNotFound Exception.

我已经阅读了有关 Route :: pattern()的方法,但是由于有很多路线,我将为此付出很多努力.所以,如果有的话,我想要的是一种更好的方法.

I have read about the Route::pattern() way of doing it but since there are many routes i'll have to put in a lot of efforts for that. So, what i want is a better way of doing it, if there is any.

推荐答案

为了使路由不区分大小写,您需要修改路由与URL匹配的方式.在Laravel中,所有操作都发生在 UriValidator 对象中,因此您需要创建自己的验证器.

In order to make routes case-insensitive you'll need to modify the way routes are matched with the URLs. In Laravel, it all happens in UriValidator object so you'll need to create your own validator.

幸运的是,就像 Laravel 中的大多数任务一样,它并不复杂.

Luckily, like most tasks in Laravel, it's not really complicated.

首先,创建一个新的验证器类-该类与原始验证器类唯一的区别是,您将在正则表达式的末尾附加 i 修饰符,以使编译后的路由切换启用大小写-不敏感的匹配.

First, create the new validator class - the only difference between this one and the original is that you'll append the i modifier at the end of regular expression for the compiled route to switch enable case-insensitive matching.

<?php namespace Your\Namespace;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Matching\ValidatorInterface;

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));
  }
}

第二,您需要更新用于将URL匹配到路线的匹配器列表,并用您的匹配器替换原始的 UriValidator .

Secondly, you need to update the list of matchers that are used to match URL to a route and replace the original UriValidator with yours.

为此,请在 routes.php 文件顶部添加以下内容:

In order to do that, add the following at the top of your routes.php file:

<?php
use Illuminate\Routing\Route as IlluminateRoute;
use Your\Namespace\CaseInsensitiveUriValidator;
use Illuminate\Routing\Matching\UriValidator;

$validators = IlluminateRoute::getValidators();
$validators[] = new CaseInsensitiveUriValidator;
IlluminateRoute::$validators = array_filter($validators, function($validator) { 
  return get_class($validator) != UriValidator::class;
});

这将删除原始验证器,并将您的验证器添加到验证器列表中.

This will remove the original validator and add yours to the list of validators.

请记住,此代码尚未通过运行进行测试.让我知道是否有拼写错误或某些功能无法正常工作.我会很乐意为您工作:)

Keep in mind that this code has not been tested by running. Let me know if there are any typos or something doesn't work as expected. I'll be more than happy to get that working for you :)

这篇关于如何使Laravel中的路由不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆