Konstantin Bodnia
Konstantin Bodnia

Reputation: 1523

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?

Upvotes: 9

Views: 6127

Answers (2)

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31140

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

Upvotes: 2

Sebastien Horin
Sebastien Horin

Reputation: 11067

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)

Upvotes: 2

Related Questions