如何在Symfony2中使路由不区分大小写? [英] How can I make routing case insensitive in Symfony2?
问题描述
Symfony2中是否有任何允许使用大小写不敏感路由的配置?
Is there any configuration in Symfony2 that allow use of case Insensitive routing?
例如,以下路由应按原样对待相同:
For example, routes below should be treated as they are the same:
www.exmaple.com/all
www.exmaple.com/ALL
有一个对此提出了要求,但没有提及如何实现。
There is a pull request about this, but no reference how to make it happen.
推荐答案
我发现了一个在纯symfony中完成此操作的一种巧妙方法(无需apache mod_rewrite),而不必为每条路由创建不区分大小写的转发规则。
I found a nifty way to do this in pure symfony (no apache mod_rewrite) without having to create a case-insensitive forwarding rule for every route.
这利用了枝条ExceptionController。由于这种情况是在路由匹配失败(或出于其他原因引发404异常)之后发生的,因此不会破坏任何使用大写字母的现有路由网址(尽管这仍然是一个坏主意)。
This utilizes the twig ExceptionController. Because this occures after the routing has failed to match (or a 404 exception has been thrown for some other reason) it won't break any existing routing urls that use capitals (though that would still be a bad idea).
namespace Application\Symfony\TwigBundle\Controller;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
/**
* ExceptionController.
*/
class ExceptionController extends BaseExceptionController
{
/**
* Redirects 404s on urls with uppercase letters to the lowercase verion,
* then uses it's parent class to
* Convert an Exception to a Response.
*
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
*
* @return Response
*
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
{
if ( (string) $exception->getStatusCode() === '404' && preg_match('/[A-Z]/', $request->getPathInfo())) {
$lowercaseUrl = strtolower($request->getPathInfo());
if ($request->isMethod('POST')) {
return new RedirectResponse(
$lowercaseUrl,
'307'//307 status code preserves post information.
);
}
$queryString = $request->getQueryString();
return new RedirectResponse(
$lowercaseUrl.( strlen($queryString)>0 ? '?'.$queryString : '' ),
'301'//301 status code plays nice with search engines
);
}
return parent::showAction($request, $exception, $logger);
}
}
唯一的窍门是您需要配置此控制器即服务,您可以将正确的参数注入到父类的构造函数中:
The only other trick is that you need to configure this controller as a service you can inject the correct arguments into the parent class's constructor:
在services.yml
in services.yml
services:
application.custom.exception_controller:
class: Application\Symfony\TwigBundle\Controller\ExceptionController
arguments: [ "@twig", "%kernel.debug%" ]
在config.yml中:
in config.yml:
twig:
exception_controller: application.custom.exception_controller:showAction
当然,您可以将此控制器和服务定义放在任何地方
Of course, you can stick this controller and service definition anywhere
这篇关于如何在Symfony2中使路由不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!