简体   繁体   中英

Symfony routing with an API Web Service

I'm finishing the API of our web service. Now I'm thinking on how to make the route changes, so if we decide to make a new version we don't break the first API.

right now:

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

what i've 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)

That's easy, but the perfect solution would be to have the following kind of 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 it? what do you recommend us to do?

thanks!

I think the approach of having 2 routes, one with v1 on it and another with v2 is the better solution. It may sound like redoing work, but if u start to think, the reason to have to versons is that the first is imcompatible with second. So, mix the 2 logics would be an overkill. If you think in some day in the future when u can have 3 version, how do you think your logic will look like?

The better solution is to make the both separate, so if u need to stop doing support for version 1 it will be easy as remove the file for version 1. =)

How about just use the old routing rules and append .xml1/.xml2/.json1/.json2 to the end? That way you can reuse your current modules and only need to create a new view ("indexSuccess.xml1.php").

Or create a new routing class?

You should probably create a custom route collection, as shown here .

This would allow you to match any routing needs you have. You could move the version logic to the route class and route collection class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM