路由没有框架REST请求? [英] routing REST requests without framework?

查看:143
本文介绍了路由没有框架REST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读的文章:

HTTP://www.gen -x-design.com/archives/create-a-rest-api-with-php/

要学习如何构建一个REST API。在一个点上它说:假如你已经排到你的请求到正确的控制器,为用户

to learn how to build a rest API. At one point it says "Assuming you’ve routed your request to the correct controller for users"

我一直在试图找到一个教程或东西,显示如何做到这一点,但一切我已阅读建议的框架。我怎样才能做到这一点没有一个框架?

I have been trying to find a tutorial or something that shows how to do this, but everything i have read suggests a framework. How can i do this without a framework?

编辑:

我写一个REST API,我可以从不同的应用程序进行交互。我准备好上面的教程,这是有道理的居多,但我不明白究竟意味着什么路线我的要求,为用户正确的控制器。

I am writing a REST API that I can interact with from a different application. I ready the tutorial above, and it makes sense mostly, but I dont exactly understand what it means to route my request to the correct controller for users.

推荐答案

假设你正在使用的Apache ,你可以做到这一点很容易使用的组合 mod_rewrite的和一些基于PHP的逻辑。例如,在你的的.htaccess 或虚拟主机的定义,你可以借道单一处理器的所有请求,可能是的index.php

Assuming you are using Apache, you can accomplish this easily using a combination of mod_rewrite and some PHP-based logic. For example, in your .htaccess or vhost definition, you could route all requests through a single handler, possibly index.php:

# Don't rewrite requests for e.g. assets
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*+)$ index.php?q=$1 [L]

...然后在index.php文件做这样的事情:

...then in your index.php file do something like:

$target = $_REQUEST['q'];
/* parse the request and include the appropriate controller PHP */

例如,对于请求/产品/ 1234 可能会导致控制器/ products.php 正在处理包括在内。然后,该处理器可作用于产品 1234 。因为你使用REST,你不应该需要与具有查询字符串参数的原始请求予以关注。

For example, a request for /products/1234 might result in a controllers/products.php handler being included. That handler could then act on product 1234. Because you're using REST, you shouldn't need to be concerned with the original request having a query string parameter.

有多种方式来完成这听起来像你正在试图做的,这只是其中之一。最终,你去什么将取决于您的特定需求决定的。上面的图案是相当普遍然而,很多框架使用它,或者类似的东西。

There are multiple ways to accomplish what it sounds like you're trying to do, this is just one of them. Ultimately what you go with will depend on what your specific requirements dictate. The above pattern is fairly common however, many frameworks use it or something like it.

干杯

这篇关于路由没有框架REST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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