How can I make the routing case insensitive in Symfony2?

Is there any configuration in Symfony2 that allows you to use the Insensitive route?

For example, the routes below should be considered the same:

www.exmaple.com/all www.exmaple.com/ALL 

There is a pull request for this , but there is no link on how to do this.

+3
source share
3 answers

I found a great way to do this in pure symfony (without apache mod_rewrite) without creating case insensitive forwarding rules for each route.

In this case, the ExceptionController control is used. Since this happens after the routing did not match (or 404 exception occurred for some other reason), it will not violate any existing routing URLs that use capital (although this 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 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('/[AZ]/', $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 trick is that you need to configure this controller as a service in which you can enter the correct arguments into the constructor of the parent class:

in services.yml

 services: application.custom.exception_controller: class: Application\Symfony\TwigBundle\Controller\ExceptionController arguments: [ "@twig", "%kernel.debug%" ] 

in config.yml:

 twig: exception_controller: application.custom.exception_controller:showAction 

Of course, you can insert this controller and service definition anywhere

+2
source

As far as I know, this is not possible with Symfony 2. However, you must execute it using Apache mod_rewrite. See this blog post for more details.

Be sure to read the comments, as there are some errors with the initial post.

+1
source

As with Symfony2.4, you can now define a condition for your route and use the expression language to perform complexity checks. See the documentation for the router: fully custom routing with conditions .

Another solution would be to override the route compiler class to modify / expand the php compilation of your route marker:

 contact: path: /{media} defaults: { _controller: AcmeDemoBundle:Main:contact, media: organic } requirements: media: email|organic|ad options: compiler_class: MyVendor\Component\Routing\CaseInsensitiveRouteCompiler 

See the Symfony \ Component \ Routing \ RouteCompiler class .

Or, as fabpot said in a pull request comment, you can override the Request :: getPathInfo [1] method to always return a string string (use setFactory [2] to override the default request class).

* 1 github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L866

* 2 github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L402

+1
source

All Articles