简体   繁体   English

PHP REST API路由

[英]PHP REST API Routing

I have been looking at APIs and developing a REST API for a project that we are working on. 我一直在研究API并为我们正在开发的项目开发REST API。

The API only accepts connections from one source in JSON format, I understand that bit fine. API只接受来自JSON格式的一个源的连接,我理解这一点。

If understand the majority of what is being said, however I don't understand the 3rd code example down and where the routing information would go. 如果理解了所说的大部分内容,但是我不理解第三个代码示例,以及路由信息的位置。

The example they have provided is: 他们提供的例子是:

$data = RestUtils::processRequest();

switch($data->getMethod)
{
    case 'get':
        // retrieve a list of users
        break;
    case 'post':
        $user = new User();
        $user->setFirstName($data->getData()->first_name);  // just for example, this should be done cleaner
        // and so on...
        $user->save();
        break;
    // etc, etc, etc...
}

The part I am unsure on is how to accept the original request ie /get/user/1 - how do you route that to the correct part of the script. 我不确定的部分是如何接受原始请求,即/ get / user / 1 - 如何将其路由到脚本的正确部分。

If there has been another SO question (I have searched for quite some time) or any further educational examples please do point me in the right direction. 如果有另一个SO问题(我已经搜索了很长时间)或任何进一步的教育示例,请指出我正确的方向。

Update 更新

I have found a few routing PHP classes out there, but nothing thats just small and does what it says on the tin, everything seems to do routing + 2000 other things on top. 我已经找到了一些路由PHP类,但没有什么只是小而且做了它在锡上的说法,一切似乎都在做路由+ 2000其他事情。

I now have all the classes I need for this project named as I wish to access them from the URI ie: 我现在拥有这个项目所需的所有类,因为我希望从URI访问它们,即:

/data/users /data/users/1 /hash/users /hash/users/1 /put/users/1?json={data} / data / users / data / users / 1 / hash / users / hash / users / 1 / put / users / 1?json = {data}

So all of these should use the users class, then one of the data, hash or put methods passing anything additional after that into the method as arguments. 因此所有这些都应该使用users类,然后将data,hash或put方法之一传递给方法作为参数。

If anyone could just explain how that bit works that would be a huge help! 如果有人能够解释这一点是如何工作的那将是一个巨大的帮助!

Thanks :) 谢谢 :)

From the outset it looks like the website you've pointed out does not include a router or a dispatcher. 从一开始,您看起来就像您指出的网站不包括路由器或调度员。 There are plenty of PHP5 frameworks around which include a route and/or a dispatch or some description. 有很多PHP5框架,包括路由和/或调度或一些描述。 ( http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks#PHP ) http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks#PHP

A router would be a class which have a list of predefined routes these could be really basic or quite complex, all depends on want you want to do. 路由器将是一个具有预定义路由列表的类,这些路由可能非常基本或非常复杂,所有这些都取决于您想要做的事情。 A good REST router IMO would look something like this: 一个好的REST路由器IMO看起来像这样:

:module/:controller/:params

And then the router would then router to the correct action based on the HTTP request (GET, POST, PUT, DELETE, OPTIONS) 然后路由器将根据HTTP请求(GET,POST,PUT,DELETE,OPTIONS)路由到正确的操作

public function getAction($id) {
    // Load item $id
}

In your case, you will need a redirect rule that will send the request to something like this index.php?user=id. 在您的情况下,您将需要一个重定向规则,该规则将请求发送到类似index.php?user = id的内容。 Then you can process the get request. 然后您可以处理get请求。

The best solution I found for php REST architecture (including routing) is: 我发现的PHP REST架构(包括路由)的最佳解决方案是:

http://peej.github.com/tonic/ http://peej.github.com/tonic/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM