好的PHP Rest Api库 [英] Good PHP Rest Api library

查看:80
本文介绍了好的PHP Rest Api库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个跨平台系统,我需要制作一个rest API来将它们捆绑在一起.我在PHP方面拥有丰富的经验,我想将此用于此服务.

I am developing a cross-platform system and I need to make a rest API to tie them together. I have long experience in PHP and I want to use this for this service.

我可以100%手动开发API,但是我希望那里有一些很棒的库可以简化我的开发.

I could develop a API 100% manually, but I am hoping there is some great libraries out there that could ease my development.

有人对这样的库有任何经验吗?有什么可以推荐的吗?

Does anyone have any experience with libraries like this? Anything you could recommend?

推荐答案

对于这个问题,我获得了徽章Popular question,所以我觉得现在该是我详细说明如何进行REST解决方案的时候了.

I got the badge Popular question for this question, so I feel it's about time I elaborate how I did my REST-soluton.

我确实查看了该REST Api的Laravel,Sympfony2和Codeigniter.他们都有我喜欢的元素,而我不喜欢的元素.我主要关心的是如何进行身份验证,因为我有一个相当复杂的算法,我的用户可以使用Google或Facebook提供的应用程序的access_token或access_tokens登录.我还希望完全控制我的框架,并且上述框架具有一些我认为不必要且难以解决的要素.因此,我决定制作自己的REST解决方案.它并不像人们期望的那么难,它可以通过多种方式来完成.我这样做的方式需要一些有关OOP编程的知识.

I did look at both Laravel, Sympfony2 and Codeigniter for this REST Api. They all had some elements I liked and some I disliked. My main concern was how to do the authentication because I had a rather complex algorithm where my users can log in with the apps' access_token or access_tokens served by Google or Facebook. I also perfer being in complete control of my framework and the frameworks mentioned above had some elements I felt unnecessary and hard to work around. Because of this I decided to make my own REST-solution. It is not as hard as one might expect, and it can be done in several ways. The way I did it requires some knowledge about OOP-programming.

Okey,因此开始制作了一个称为REST的基类.此类负责处理每个调用的所有共同点.像身份验证一样,解析方法的请求路径,检查access_token等.

Okey, so starting out a made a base-class called REST. This class takes care of all the stuff that is in common for every call. Like authentication, parsing the requested path to a method, checking access_token etc.

此类中的核心内容之一是请求的路径以及如何将其转换为方法.我这样做是受Laravel启发的.我有一个key => value的数组,其中键是它应该匹配的url,值是要调用的实际方法.我还包括了Lavavel解析URL中的变量的方式,如下所示:

One of the central things in this class is the requested path and how this is translated into a method. I did this inspired by Laravel. I have a array with key => value where the key is the url it should match and the value is the actual method to call. I also included the way Lavavel parses variables in the URL like so:

'/user/(:id)' => 'user_id',

这将匹配任何/user/[number].它还会检查这是什么类型的请求,因此,如果这是一种简单的get方法,它将尝试调用get_user_id.调用该方法时,任何使用(:id)解析的内容都将用作参数(因此它实际上是在调用get_user_id($id)).

This would match any /user/[number]. It also checks what type of request this is, so if this is a simple get-method it would try to call get_user_id. Anything parsed with (:id) would be used as an argument when calling that method (so it is actually calling get_user_id($id)).

验证后,将评估实际的方法调用.我不需要REST类本身中的所有方法(例如get_user_id),因此我在扩展REST类的不同控制器中分解了这些方法.这是通过查看所请求的URL来完成的.如果它是/user/(:id),脚本将检查是否有一个名为userController.php的控制器.如果存在,请检查要调用的方法是否存在.如果是这样,请检查参数数量是否与我们的参数匹配.如果一切都很好,请执行该方法,如果没有返回错误消息.制作这样的API时,结构和错误消息非常重要.

After authentication the actual method-call gets evaluated. I did not want all the methods (like get_user_id) in the REST-class itself, so I broke these up in different controllers that extends the REST-class. This is done by looking at the url being requested. If it is /user/(:id) the script will check if there is a controller named userController.php. If it exists, check if the method we are going to call exists. If it does, check if the number of arguments matches what we have. If everything is good, execute the method, if not return an error message. Structure and error-messages are very important when making a API like this.

在不同的控制器中,我调用REST类的构造函数以获取身份验证,URL解析等.这里最棘手的部分是我不想做:

In the different controllers I call the constructor for the REST-class to get the authentication, parsing of the url etc resolved. The tricky part here is that I did not want to do:

$controller = new MyController();
$controller->printResponse();

在每个控制器的底部.因此,我做了一个小技巧,并编写了一个名为run.php的脚本,该脚本可以为每个控制器类动态地执行此操作.在包含run.php之前,我只需做$path = explode('/',__FILE__);即可保存控制器的路径.这在运行脚本中使用.运行脚本如下所示:

In the bottom of every controller. So I made a little hack and a script called run.php that does this dynamically for every controller-class. Before I include the run.php I save the path for the controller by simply doing $path = explode('/',__FILE__);. This is used in the run-script. The run-script looks like this:

// Splitting the file-name, removing the extension
$name = explode('.',$path[count($path)-1]);

// Uppercasing the first letter to be nice and OOP-ish
$classToCall = ucfirst($name[0]);

// Creating a new instance
$controller = new $classToCall();

// Logging
$controller->doLog();

// Printing the final response
$controller->printResponse();

我发现这对于我想要构建API来说是一个完美的解决方案.我可以通过在将URL解析为方法的数组中提供它来轻松添加新方法,并且我可以在分离得很好的控制器中添加新方法,以实现最大程度的整洁.

I found this to be a perfect solution for how I wanted to build my API. I can easily add new methods by supplying it in the array that parses urls to methods, and I can add new methods in the nicely broken apart controllers for maximum cleanness.

有些人可能认为这工作太多,但实际上只花了我几个小时就将其启动并运行.我还称其为高度动态,因为我可以添加新的控制器,并且系统将识别出它们是否为有效的url模式.

Some people might think this is too much work, but it actually took me only a few hours to get it up and running. I'd also call this highly dynamic as I can just add new controllers and the system will recognize them if they are valid url-patterns.

一些友好的建议.

如果您决定采用类似于该解决方案的方法,那么这些可能是一些不错的技巧.在每个控制器中,执行以下操作:

If you decide to go with something resembling this solution, these can be some good tips. In each controller, do something like this:

public function __construct() {
    // Loading the class-name, setting it in the REST-class, so we can check if it holds the method being called
    $this->className = get_class($this);

    // Calling RESTs constructor
    parent::__construct();
}

我们将需要存储当前正在使用的类.可能是UserController或类似的内容.

We will need to store what class we are currently working from. This would be UserController or something like that.

然后在REST类中,我可以使用此变量来检查此控制器中是否确实存在被调用的实际方法.我这样做是这样的:

In the REST-class I can then use this variable to check if the actual method getting called does exist in this controller. I've done that this way:

// Checking if the method exists
if (method_exists($this->className,$method_name)) {
    // Check to see if we have the required number of arguments represented
    $ReflectionClass = new ReflectionClass($this->className);

    if ($ReflectionClass->getMethod($method_name)->getNumberOfParameters() == count($this->methodUrl['args'])) {
        $this->response['response'] = call_user_func_array(array($this, $method_name), $this->methodUrl['args']);

我希望可以帮助大家.

快乐codin'

这篇关于好的PHP Rest Api库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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