Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile laravel's REST API

There's a great extension to laravel: debugbar. But what if I have an REST API. With like no frontend. How do I profile this type of application?

like image 997
Konstantin Bodnia Avatar asked Jan 06 '15 14:01

Konstantin Bodnia


2 Answers

Instead of returning your response, send it to a view:

return \View::make('debug', ['data' => $response]);

instead of

//return response()->json($response); 

(don't forget to create the view where you echo your data)

like image 151
Sebastien Horin Avatar answered Oct 11 '22 01:10

Sebastien Horin


You can try this Middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\JsonResponse;

class ProfileJsonResponse
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (
            $response instanceof JsonResponse &&
            app()->bound('debugbar') &&
            app('debugbar')->isEnabled() &&
            is_object($response->getData())
        ) {
            $response->setData($response->getData(true) + [
                '_debugbar' => app('debugbar')->getData(),
            ]);
        }

        return $response;
    }
}

Reference https://github.com/barryvdh/laravel-debugbar/issues/252

like image 26
Mahmoud Zalt Avatar answered Oct 11 '22 01:10

Mahmoud Zalt