为什么网络路由并不总是自动化的?
手动网络路由的目的是什么?为什么不是每个人都自动在 URL 和模块/方法/函数名称之间进行映射?
我认为您可以从全自动映射开始,然后如果您想以更改 URL 的方式进行重构,而不破坏现有 URL,则可以使用 Apache mod_rewrite 或 mod_redirect 或其他任何内容。
What is the purpose of manual web routing? Why doesn't everyone just automatically map between URLs and module/method/function names?
I would argue that you can start with fully automatic mapping, and then you can just use Apache mod_rewrite or mod_redirect or whatever if you want to refactor in a way that would change URLs, without breaking existing URLs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用手动路由而不是自动路由有两个主要原因。
手动路由允许您按照应有的方式使用 REST。这意味着同一个 URL 可以访问 4 个不同的操作,通过使用的 HTTP 方法(POST、GET、PUT、DELETE)进行区分。使用自动路由,您将公开底层函数的方法名称,因此,您将拥有 4 个不同的 URL。
手动路由还允许您生成更多搜索引擎友好的 URL。这通常是使用 slugify 方法来实现的,但是手动路由允许您忽略这些额外的信息,而只关注 URL 的 ID 部分,以正确地将您路由到特定资源。
另一个纯粹是装饰性的原因是 URL 看起来更好。这有关系吗?有些人可能会这么认为。
There are two main reasons for using manual routing, over automatic.
Manual routing allows you to use REST as it is meant to be used. This means that the same URL can access 4 different actions, distinguished by the HTTP method used (POST, GET, PUT, DELETE). With automatic routing, you would be exposing the method names of your underlying functions, therefore, you would have 4 different URLs.
Manual routing also allows you to produce more search-engine friendly URLs. This is usually achieved using the slugify method, but the manual routing allows you to ignore this extra information, and just concentrate on the ID part of the URL to correctly route you to the specific resource.
Another reason, which is purely cosmetic, is that the URLs just look better. Does that matter? Some may think so.
您可以提出的一个论点是,您的应用程序提供的 URL 命名空间是其用户界面的一部分,因此管理它的路由规则属于应用程序源代码的其余部分。此外,对于 Ruby on Rails,路由配置都只是 Ruby 代码,因此如果需要,您可以在其中拥有比单独的 Apache 模块所允许的更复杂的业务逻辑。
One argument you could make is that the URL namespace your application presents is part of its user interface and therefore the routing rules that govern it belong with the rest of the application's source code. Also, in the case of Ruby on Rails, the routing configuration is all just Ruby code, so you can have more complex business logic in there if required than Apache modules alone would allow.
对于 Restful HTTP,URL 很重要。因此,如果您只使用自动 URL,则会出现很多不太好的 URL。例如,您可以通过区分 GET 和 POST,在 Play 中对 Users.edit() 和 User.save() 使用相同的 URL /udpateUsers。
此外,您可以支持数据绑定,例如 /users/show/1 以显示 id 1 的用户。
然而,Play 中的默认设置是自动执行的。
For restful HTTP the URL are important. So if you only use automatic URLs you have a lot of not so nice URLs. For example you can use the same URL /udpateUsers for Users.edit() and User.save() in Play by differencing between GET and POST.
Furthermore you can support data-binding like /users/show/1 to show the User with id 1.
However the default in Play is to do it automatic.
一般网络意义上的“模块”是什么?
Apache 的默认设置是映射到文件系统,因为这是所有运行 Apache 的系统都拥有的唯一东西之一。
What is a "module" in a generic web sense?
Apache's default is to map to a filesystem, because that's one of the only things that that all systems running Apache have.