[英]Routing in RESTful API in PHP
I am a bit familiar with PHP MVC. 我对PHP MVC有点熟悉。 Say, I have a controller like so:
说,我有一个像这样的控制器:
class Customers{
public function method1( param1, param2, param3, ..., param_n ){
}
}
In my bootstraping page, I can grab a browser URL like so: 在我的bootstraping页面中,我可以像这样获取浏览器URL:
$url = explode('/', filter_var(rtrim( $_GET['url'], '/' ), FILTER_SANITIZE_URL));
I do $controller = $url[0]
and $method = $url[1]
. 我做
$controller = $url[0]
和$method = $url[1]
。 Any other elements in $url
after the second index are parameters and can be collected into an array variable, say $params
. 第二个索引后
$url
任何其他元素都是参数,可以收集到数组变量中,比如$params
。 Then I route to the relevant controller method and parameters like so: 然后我路由到相关的控制器方法和参数,如下所示:
call_user_func_array([$controller, $method], $params);
PLEASE NOTE: Above code is for illustration purposes. 请注意:以上代码仅供参考。 I always do checks in real-life situations.
我总是在现实生活中做检查。 Those checks are not shown here, so do not use the above examples for serious projects.
这些检查没有在这里显示,因此不要将上述示例用于严肃的项目。
Now, I want to implement a RESTful API using MVC pattern. 现在,我想使用MVC模式实现RESTful API。 What I already know:
我所知道的:
$_GET['url']
is out of it. $_GET['url']
不在其中。 $_SERVER['REQUEST_URI']
$_SERVER['REQUEST_URI']
$_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_METHOD']
How do I route to an endpoint, for example, customers/{12345}/orders
to get the orders of a particular customer with id 12345
? 如何路由到端点,例如
customers/{12345}/orders
以获取ID为12345
的特定客户的订单?
How can I do this? 我怎样才能做到这一点?
The quickest way to achieve what you want would be to just use FastRoute . 实现您想要的最快捷方式就是使用FastRoute 。
But where is fun in that :) 但那里的乐趣在哪里:)
That said, I am a but confused about the premise of yours.
也就是说,我对你的前提感到困惑。 Is it a REST API, which will be consumed by some other code (like in a phone or a 3rd party web app), or is it a proper website, where you just want to get pretty URLs?
它是一个REST API,它将被其他一些代码(如电话或第三方Web应用程序)使用,或者它是一个合适的网站,您只想获得漂亮的URL?
Because if it's the former case, then making fancy URL parsing is completely pointless.
因为如果它是前一种情况,那么制作花哨的URL解析是完全没有意义的。 Instead of messing round with URLs, you should be reading this article .
你应该阅读这篇文章而不是乱七八糟的URL。 Real REST API does not need a fancy URL parsing.
真正的REST API不需要花哨的URL解析。
I will assume that what you are actually makingis a proper website, but with pretty URLs. 我会假设你实际制作的是一个合适的网站,但有漂亮的网址。
First you would have to implement a routing mechanism, which takes a list of regexp patterns, matches them agains your provided URL (which you could be extraction from $_GET['url']
or maybe $_SERVER[REQUEST_URI]
(your code actually wouldn't care from where the URL was gathered ... you shouldn't be accessing superglobals inside functions/classes). 首先,你必须实现一个路由机制,它采用一个正则表达式模式列表,再次匹配你提供的URL(你可以从
$_GET['url']
提取或者可能是$_SERVER[REQUEST_URI]
(你的代码实际上不会关注URL的收集位置......你不应该访问函数/类中的超全局。
A simple version of this is explained in this answer . 在这个答案中解释了这个的简单版本。 I am to lazy to rewrite it all =P
我懒得重写它全部= P.
The second (and highly optional) part is creating code, that would take a human-readable route notation (like: /users/{id|[0-9]+}
as an example) and turning it into the regular expression, which can be consumed by your routing mechanism. 第二个(也是高度可选的)部分是创建代码,它将采用人类可读的路由符号(例如:
/users/{id|[0-9]+}
作为示例)并将其转换为正则表达式,可以通过您的路由机制被消耗 。
If you decide to have the human-readable notations, then there are two major directions: 如果您决定使用人类可读的符号,那么有两个主要方向:
As for "how the end result might look like", you can probably look at the code sample here . 至于“最终结果可能如何”,您可以在这里查看代码示例。 That would illustrate one of the available option for the router's public interface.
这将说明路由器公共接口的可用选项之一。
TL;DR TL; DR
Your question is vague and it is hard to understand what exactly would be helpful for you. 你的问题很模糊,很难理解对你有什么帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.