在路由 URL 时使用 index.php?q=path/而不是 index.php/path/的优缺点? [英] Pros/cons of using index.php?q=path/ instead of index.php/path/ when routing URLs?

查看:45
本文介绍了在路由 URL 时使用 index.php?q=path/而不是 index.php/path/的优缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将路由映射到文件的简单方法,我遇到了两种方法.

I'm writing a simple method to map routes to files and I've come across two ways to do it.

第一个,我猜被大多数框架使用,是使用 $_SERVER['REQUEST_URI'] 变量来提取 index.php 之后的所有内容:

The first, and I guess used by most frameworks, is using the $_SERVER['REQUEST_URI'] variable to extract everything after index.php:

RewriteRule ^(.*)$ index.php [QSA,L]

Drupal 中使用了第二种方式,路由只是作为查询字符串传递.

The second way is used in Drupal, and the route is simply passed as a query string.

RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

现在,Drupal 方式"对我来说似乎简单得多.使用另一种方法,您必须在 $_SERVER['REQUEST_URI'] 和 $_SERVER['SCRIPT_NAME'] 上使用explode",然后使用类似 array_diff_assoc 的东西来删除脚本名称和子目录名称(如果有的话).没有那么多工作,但是如果使用 Drupal 方式您可以简单地提取 $_GET['q'] 值,为什么没有人这样做呢?如果有的话,有什么缺点?

Now, the "Drupal way" seems a lot simpler to me. With the other method you'd have to use "explode" on both $_SERVER['REQUEST_URI'] and $_SERVER['SCRIPT_NAME'] and then use something like array_diff_assoc to remove the script name and subdirectory name, if there is one. It's not THAT much work, but if with the Drupal way you can simply extract the $_GET['q'] value, why nobody does it that way? What are the disadvantages, if any?

谢谢.

推荐答案

使用 q 参数的缺点是,如果不重写 URL,URL 将看起来像...

The disadvantage of using a q param is, without URL rewriting the URL will look like...

http://domain.com/?q=something

...相对于清洁器 (IMO)...

...as opposed to the cleaner (IMO)...

http://domain.com/index.php/something

这篇关于在路由 URL 时使用 index.php?q=path/而不是 index.php/path/的优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆