5

I need to have different routes pointing to the same bundle. Ex: /mkt/contacts and /crm/contacts should pointing to the same contact bundle.

Why? I am developing a platform (one symfony2 project), that have many applications (A marketing application, a CRM application and more. Each application is a group of modules (bundles), some of them are shared between applications (Like ContactsBundle).

What I need to do is that the route prefix of the shared bundles are relative to the current application, so if the user is in the Marketing Application (/mkt/) then the route prefix to the ContactBundle should be /mkt/contact. But if it is in the CRM application it should be /crm/contacts

I think I can create two route resources in routing.yml like so:

route1:
    resource: "@Contactundle/Resources/config/routing.yml"
    prefix:   /crm/contact
route2:
    resource: "@ContactBundle/Resources/config/routing.yml"
    prefix:   /mkt/contact

The biggest problem is in the views when using path function. How do I know the name of the route? The only possible solution I can think of is to create some twig extension which checks the current pathinfo and return the correct route prefix.

Any better ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
brpaz
  • 3,618
  • 9
  • 48
  • 75

2 Answers2

1

you should use one route but use mkt and crm as a variable.

contact:
    path:      /{ prefix }/contact
    defaults:  { _controller: YourContactBundle:Contact:contact }

Then in twig or wherever you want

{{ path('contact', { 'prefix': prefix } ) }}

You should retrieve the variable contact by session or by giving parameters to the route.

As it's just some few bundles who are in common, i would recommend to give the parameter. In home of the market app, i would call the contact form by {{ path('contact',{ 'app_prefix' : constant_prefix_mkt} ) }}

goto
  • 7,908
  • 10
  • 48
  • 58
0

You don't want to load all your bundle routing file two times with differents prefix

Try:

app/routing.yml

crm_contact:
    path:      /crm/contact
    defaults:  { _controller: YourContactBundle:Contact:contact }
mkt_contact:
    path:      /mkt/contact
    defaults:  { _controller: YourContactBundle:Contact:contact }

then in your twig:

{{ path(app.request.attributes.get('_route')) }}
Vincent Barrault
  • 2,906
  • 1
  • 19
  • 17