1

I have some fairly complex routing rules, that are only achievable with custom code, and not with the default router.

The router has also to take into account the domain name.

Is it possible (and how) to define my own Router class, that would have a method accepting a Request and returning the bundle/controller name?

j0k
  • 22,600
  • 28
  • 79
  • 90
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • possible duplicate of [Custom route class](http://stackoverflow.com/questions/8384660/custom-route-class) – Peter Bailey Jul 06 '12 at 17:17
  • @Peter Bailey: I've read this question, I've even offered and awarded a bounty for it. This question is more specific, as I'm not routing only based on the request URI, but also on the domain name. – BenMorel Jul 07 '12 at 23:06
  • Well, there are some bundles that do routing based on the domain, but it's also being put into the core in Symfony 2.2 https://github.com/symfony/symfony/pull/3378. Also, another answer on SO might fit http://stackoverflow.com/questions/5366234/symfony2-routing-route-subdomains – Peter Bailey Jul 10 '12 at 14:32

4 Answers4

4

To achieve what you're asking, you don't need to completely redefine the router

You can simply write your own class that implements UrlMatcherInterface. If you want to be able to generate URLs that match your scheme as well, you'll have to create another class which overrides UrlGeneratorInterface as well.

In order to take into account the domain name, you'll need to use RequestContext, which is passed to their constructors (it's not well documented, but check Router::get{Matcher,Generator} for the details).

Once your classes are prepared, you can inject them into the router simply by overriding the parameters "router.options.generator_class" and "router.options.matcher_class" in your bundle.


However, it may not be the best approach for what you want - overriding parts of the router like that requires a lot of care to preserve all of the caching.

You might want to consider using Symfony2's normal router, but handing it different route files depending on what the request's domain is. This can be done easily if you configure your web server to set a variable or execute a different front controller depending on the domain name.

Using that information, you can then load a different 'environment' per-request, with the only difference between the different environments being that they use different routing files.

Lachlan Pease
  • 730
  • 5
  • 11
  • How about extending Symfony Routing's UrlMatcher, and injecting that? This way you can keep the original routing logic, and inject your own if necessary. I haven't tested it yet, but I want to do this to check the browser type for specific routes, so they are available to specific (e.g. mobile) browsers. – GergelyPolonkai Aug 07 '12 at 19:13
  • That's exactly what I suggest in the first part of my answer (the router.options.matcher_class parameter is what you use to inject your custom UrlMatcher). But remember, if you want your generated URLs to work properly, you have to override the UrlGenerator as well. – Lachlan Pease Aug 08 '12 at 00:56
1

After studying Matthias Noback's tutorial, I have made a slight modification for my CRUD routing builder.

Before and after using CrudLoader can be seen here in routing/crud/acompetencies.yml

It is just a workaround or misuse of resource as you can see in this CrudLoader class.

I don't know if it is right or bad practice. It seems to work well.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
epsi
  • 11
  • 1
0

refer sonata admin bundle which is having custom routing class classes

Anil Gupta
  • 2,329
  • 4
  • 24
  • 30
0

symfony 2.5 requires parameter for custom matcher: router.options.matcher_base_class and class which implements Symfony\Component\Routing\Matcher\RequestMatcherInterface

Are Butuv
  • 292
  • 3
  • 10