Issue
For Symfony2, if you would like to setup different url based on environment but using same action in controller, what should we do?In my case, I would have liked to setup different url based on different country enviroment but would have liked to use same action becaseu the functionality itself is completely same.
Solution
Setup routing.yml for each environment.Symfony2 porvides mailnly 2 ways to setup routing configuration - routing.yml and annotation on controller.
I think using annotation is better way because you can easily check functionality and routing relation in controller class.
However if you would like to setup different url based on different environment, using routing.yml is better way.
Ok now I will show you my approach....
1) You should prepare routing_shared.yml for common shared routing between environment. 2) You should preare routing.yml for rach enviroment which is depends on the environment, like routing_env_1.yml, routing_env_2.yml, etc.
And the enviroment based routing.yml should import routing_shared.yml. like...
# routing_env_1.yml # See controller and action is same as env2! env_1: path: /env1_action/ defaults: { _controller: SomeBundle:ControllerClass:action } _shared: resource: "routing_shared.yml"
# routing_env_2.yml # See controller and action is same as env1! env_2: path: /env2_action/ defaults: { _controller: SomeBundle:ControllerClass:action } _shared: resource: "routing_shared.yml"
3) Setup config.yml so that Symfony2 can load routing.yml based on environmeny.
# app/config/config.yml framework: secret: xxxxxxxxxx form: true csrf_protection: true # some other configuration...... # !!!IMPORTANT PART!!! # prameter routing_env should be passed by parameters.yml # the parameter name is not important, if you already have parameter based on environment and if you can reuse it, you should use that parameter. router: { resource: "%kernel.root_dir%/config/routing_%routing_env%.yml" } # some other configuration......4) Just deploy parameters.yml for each environment, and Symfony2 load proper routing configuration for each environment :D
In this example setup case, /env1_action/ is valid path for env1, and /env2_action/ is valid path for env2.
In my case, I prepare parameters.yml.env1, parameters.yml.env2 and deploy it using capistrano+capifony.
コメント