简体   繁体   English

使用API​​ Web服务进行Symfony路由

[英]Symfony routing with an API Web Service

I'm finishing the API of our web service. 我正在完成我们的Web服务的API。 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. 现在,我正在考虑如何更改路线,因此,如果我们决定制作一个新版本,我们不会破坏第一个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. 我认为拥有2条路由的方法是更好的解决方案,一条带有v1,另一条带有v2。 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. 这听起来像是在重做工作,但是如果您开始思考,必须使用verson的原因是第一个与第二个不兼容。 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? 如果您认为将来某个时候您可以拥有3个版本,那么您认为自己的逻辑会如何?

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. =) 更好的解决方案是将两者分开,因此,如果您需要停止对版本1的支持,则很容易删除版本1的文件。=)

How about just use the old routing rules and append .xml1/.xml2/.json1/.json2 to the end? 仅使用旧的路由规则并将.xml1 / .xml2 / .json1 / .json2追加到末尾怎么样? That way you can reuse your current modules and only need to create a new view ("indexSuccess.xml1.php"). 这样,您可以重用当前的模块,而只需创建一个新视图(“ 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. 您可以将版本逻辑移至路由类和路由收集类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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