在“清理”之后如何访问查询字符串参数。您的PHP REST API? [英] How to access query string parameters after "Cleaning" your PHP REST API?
问题描述
为了提供一些背景信息,我最初使用的api如下所示:
To give some context, I initially started out with my api looking like this:
http://myserver/api.php?action=projects
在我的api.php文件中,我有一个简单的if语句来检测操作,然后做出相应的反应:
In my api.php file I have a simple if statement to detect the action, and then react accordingly:
if (isset($_GET["action"])
{
switch($_GET["action"])
{
case "projects"
dostuff();
break;
case "otherstuff"
dootherstuff();
break;
...
}
}
但是,我当然想清理它,所以我可以转到以下网址:
However, I of course wanted to clean this up, so I could go to this url:
http://myserver/api.php/projects
并获得相同的结果。通过阅读以前的SO帖子来实现此目的:
and get the same outcome. I was able to accomplish this by reading up on a previous SO post:
最终通过在我的根目录中创建一个.htaccess文件并使用重写引擎来使其正常工作:
And ultimately got it working by making an .htaccess file in my root directory and using the rewrite engine like so:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L]
这很好用!除了一个问题,因为我使用的是干净的URL,所以它似乎不再能够识别GET参数的存在。
This worked great! Except for one problem, because I am using the clean URL, it no longer seems to recognize the GET parameter exists.
我发现我可以使用
$ _SERVER ['REQUEST_URI'];
I've found I can use $_SERVER['REQUEST_URI'];
然后解析URL以获取我的参数。我一开始引入多个参数,就看不到如何解决这个问题。
And then parse the URL to get my parameters. I don't see how this could work out though once I start introducing multiple parameters. Is there something I'm missing here, or do I need to look at it a different way and change how I format the parameters instead?
推荐答案
是我在这里缺少的东西,还是需要以另一种方式看待它并更改我设置参数格式的方式? >
您可以使用以下规则:
You can use these rules:
RewriteEngine On
RewriteRule ^(?:action|api\.php)/([^/.]+)/?$ api.php?action=$1 [L,NC,QSA]
这篇关于在“清理”之后如何访问查询字符串参数。您的PHP REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!