问题描述
我已经使用Luracast Restler 3 RC 6( https://github.com/Luracast/Restler/tree/3.0.0-RC6 ),现在我想创建一个登录到我的API的日志,以便记录对API的每个请求.我试着几乎到处都在寻找答案,但是没有运气.所以我的问题是:
I have created an API with Luracast Restler 3 RC 6 (https://github.com/Luracast/Restler/tree/3.0.0-RC6) and now I want to create a logging into my API so that every request made for the API will be logged. I've tried to search for the answer pretty much everywhere, without luck. So my question is:
如何在Restler 3中创建一个日志记录类/函数,每次发出请求时都会调用该类/函数?应该以某种方式实现到路由中还是什么?我需要类似的东西:
How can I create a logging class/function into Restler 3, which gets called everytime when a request is made? Should it be somehow implemented into routing, or what? I need something like:
logging($route, $http_method, $format, $api_key, $parameters);
此函数/类应获取正在发出的请求的所有可能信息,并将其插入到logging
表中,如下所示:
This function/class should get every possible information of the request being made and insert it into logging
table, something like this:
+----+---------------------+------------+----------+----------------+--------+---------------------------+
| id | log_time | ip_address | api_key | route | method | parameters |
+----+---------------------+------------+----------+----------------+--------+---------------------------+
| 1 | 2015-08-06 14:32:54 | 127.0.0.1 | ASDFasdf | /v1/users/list | GET | all_given_parameters_here |
+----+---------------------+------------+----------+----------------+--------+---------------------------+
我并不是要您为我创建用于记录的函数/类,我只需要某种有关如何以及在何处执行此操作的指南,或者甚至可能吗?
I'm not asking you to create me a function/class for logging, I just need some kind of a guidance on how and where should I do this, or is it even possible?
忘了提一下,我还需要记录Method,该方法用于给定的路由:GET,POST,PUT,PATCH或DELETE.也许也是使用的格式(JSON或XML).
Forgot to mention I need to log Method also, that which method is being used for the given route: GET, POST, PUT, PATCH or DELETE. Maybe also the format being used (JSON or XML).
我假设我应该以某种方式扩展Luracast \ Restler \ Routes.php Routes->find()
,但是如何以及在何处?
I assume I should extend Luracast\Restler\Routes.php Routes->find()
somehow somewhere, but how and where?
推荐答案
您可以为此使用事件处理程序(在调用阶段).参见下面的示例
You can use the event handler (at call stage) for this. See the following example
use Luracast\Restler\Restler;
use Luracast\Restler\User;
$r = new Restler();
$r->onCall(function () use ($r) {
// Don't log Luracast Restler Explorer recources calls
if (!preg_match('/resources/', $r->url)) {
$info = array(
'base' => $r->getBaseUrl(),
'method' => $r->requestMethod,
'url' => $r->url,
'route' => $r->apiMethodInfo->className.'::'.$r->apiMethodInfo->methodName,
'version' => $r->getRequestedApiVersion(),
'data' => $r->getRequestData(),
'ip' => User::getIpAddress(),
);
print_r($info);
}
});
$r->addAPIClass('Say');
$r->handle();
用记录功能替换print_r
这篇关于如何在PHP Restler 3中记录所有请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!