How to log all requests in PHP Restler 3?
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 registration in my API so that every request made to the API will be registered. I've tried to find the answer almost everywhere, with no luck. So my question is:
How do I create a logging class / function in Restler 3 that gets called every time it is requested? Does this have to be implemented in some way in routing or what? I need something like:
logging($route, $http_method, $format, $api_key, $parameters);
This function / class should get all possible information about the executed query and insert it into the table logging
, 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 am not asking you to create a function / class for me to do logging, I just need some guidance on how and where I should do this, or is it even possible?
EDIT: Forgot to mention that I also need the log Method, which method is used for a given route: GET, POST, PUT, PATCH or DELETE. Maybe the format used (JSON or XML).
EDIT: I am guessing I have to extend Luracast \ Restler \ Routes.php somehow Routes->find()
, but how and where?
source to share
For this you can use an event handler (at the call stage). See next 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();
Replace with print_r
your logging function
source to share