是否使用基于文件的 URI
当然,传统的方法是让文件系统和网络服务器计算出 url。 EG,www.example.com/index.html -> /var/www/public_html/index.html
但我注意到一种趋势,与让网络服务器为你做映射相反,你自己做(例如,Rails 及其路由配置文件,所以 /index 是映射到 app/controllers/index.rb 或您设置的位置)。
现在我意识到后一种方法的优点之一是,如果服务器开始无法执行某些内容或w/e,那么没有人可以查看您的源代码;但对于已编译的 CGI,这显然不是问题。
是否有任何真正的理由(除了与 REST 相关的想法之外)自己解析 URL? (我感觉这比让 Apache 做要慢。)
Of course, the traditional method is to let the filesystem and your webserver work out the urls. E.G., www.example.com/index.html -> /var/www/public_html/index.html
But I've noticed a trend in which, as opposed to letting the webserver do the mapping for you, you do it yourself (e.g., Rails and its routes config file, so /index is mapped to app/controllers/index.rb or w/e you set it up to be).
Now I realize that one of the advantages of the latter approach is that nobody can view your source code if the server starts failing to execute stuff or w/e; but for compiled CGIs, this obviously isn't a problem.
Is there any real reason (REST-related ideas aside) to parse the URLs yourself? (I have a feeling that it's slower than letting Apache do it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 URL 后面添加代码的最大原因是它允许不受文件支持的 URL 响应查询。您可能希望在磁盘上为您可能想要提供服务的每个 URL 都有一个 CGI,但您可能并不这样做。您可以执行
/products.cgi?productid=12
,但更适合人类使用的 URL 是/products/12
。The biggest reason to have code behind a URL is that it allows URLs that aren't backed by a file to respond to queries. You may want to have a CGI on disk for every URL that you could possibly want to serve up, but you probably don't. You could do
/products.cgi?productid=12
, but a prettier URL for humans to use is/products/12
.自己定义路由(如您给出的 ruby 示例)的问题是 URI 不必指向实际文件。对于 MVC 框架,它们只是指向操作。您可以让同一个控制器处理多个操作,该控制器位于同一个类和同一个文件中。这是 很好的(ASP.NET MVC)示例,展示了路由为您提供的内容(特别是查看有关搜索的部分)。
其扩展是 URI 不指向文件,而是指向资源。这些资源可以由文件、数据库表或某个可怜的人来支持,他们手动为每个请求编写 html 并使用摩尔斯电码敲击 0 和 1。
The thing about defining routes yourself (as the ruby example you gave) is that the URIs don't have to point to actual files. In case of MVC frameworks they simply point to actions. You could have several actions be handled by the same controller, which is in the same class and in the same file. Here's a nice (ASP.NET MVC) example of what routing gives you (in particular take a look at the section on search).
The extension of that is that URIs don't point to files, they point to resources. Such resources could be backed by files, or database tables, or some poor guy who writes out html for every request by hand and taps the 0's and 1's using Morse code.