I want to log every request and response for API.
$app->hook('slim.after.router', function () use ($app) {
$request = $app->request;
$response = $app->response;
echo "<pre>";print_r($response);die;
});
Here i am getting proper request but when i tried to print out response i am getting
Slim\Http\Response Object
(
[status:protected] => 200
[headers] => Slim\Http\Headers Object
(
[data:protected] => Array
(
[Content-Type] => application/json
)
)
[cookies] => Slim\Http\Cookies Object
(
[defaults:protected] => Array
(
[value] =>
[domain] =>
[path] =>
[expires] =>
[secure] =>
[httponly] =>
)
[data:protected] => Array
(
)
)
[body:protected] =>
[length:protected] => 0
)
I am able to render proper response in API response but not getting as in logs.
Any help would be appreciated. Thank you!
use of slim.after
instead of slim.after.router
have resolved the problem.
As in the documentation
slim.after.router :- This hook is invoked after the router is dispatched, before the Response is sent to the client, and after output buffering is turned off. This hook is invoked once during the Slim application lifecycle.
slim.after :- This hook is invoked after output buffering is turned off and after the Response is sent to the client. This hook is invoked once during the Slim application lifecycle.
$app->hook('slim.after', function () use ($app) {
$request = $app->request;
$response = $app->response;
echo "<pre>";print_r($request);die;
});
which outputs :-
Slim\Http\Response Object
(
[status:protected] => 200
[headers] => Slim\Http\Headers Object
(
[data:protected] => Array
(
[Content-Type] => application/json
)
)
[cookies] => Slim\Http\Cookies Object
(
[defaults:protected] => Array
(
[value] =>
[domain] =>
[path] =>
[expires] =>
[secure] =>
[httponly] =>
)
[data:protected] => Array
(
)
)
[body:protected] => {"data":{"Authorization":"786876866","user":{"id":"1","user_role":"1"}},"status":200,"success":true,"message":"user authentication successful"}
[length:protected] => 577
)
Ofcourse you will get response object too :)