Routing Symfony Using the API Web Service

I am finishing the API of our web service. Now I’m thinking about how to change the route, so if we decide to create a new version, we won’t break the first API.

right now:

url: /api/:action param: { module: api, action: :action } requirements: sf_format: (?:xml|json) 

what i thought:

 url: /api/v1/:module/:action param: { module: api1, action: :action } requirements: sf_format: (?:xml|json) url: /api/v2/:module/:action param: { module: api2, action: :action } requirements: sf_format: (?:xml|json) 

It’s easy, but the ideal solution would be to have the following view of the route

 # Automatically redirects to one module or another url: /api/v:version/:module/:action param: { module: api:version, action: :action } requirements: sf_format: (?:xml|json) 

any ideas on how to do this? What did you recommend to us?

thanks!

+4
source share
3 answers

I think the best option is an approach with two routes: one with v1 on it, and the other with v2. This may sound like repeated work, but if you start to think, the reason you need to use poetry is because the first is incompatible with the second. Thus, mixing 2 logic would be redundant. If you think that sometime in the future, when you will have 3 versions, what do you think your logic will look like?

The best solution is to do both separately, so if you need to stop supporting version 1, it will be easy to delete the file for version 1. =)

+1
source

How easy is it to use the old routing rules and add .xml1 / .xml2 / .json1 / .json2 to the end? This way you can reuse your current modules and just need to create a new view ("indexSuccess.xml1.php").

Or create a new routing class?

+1
source

You should probably create your own route collection, as shown here .

This will allow you to match any of your routing needs. You can move the version logic to a route class and a route collection class.

0
source

Source: https://habr.com/ru/post/1304473/


All Articles