chrisjohnson00/api-profiler-bundle
Installation:
composer require chrisjohnson00/api-profiler-bundle:dev-master
Add to AppKernel.php:
new ChrisJohnson00\ApiProfilerBundle\ChrisJohnson00ApiProfilerBundle(),
First Use Case:
config.yml:
framework:
profiler: { only_exceptions: false }
/_profiler/api to view API-specific metrics (e.g., request/response times, payload sizes, headers).Debugging API Endpoints:
config.yml under api_profiler).Integration with Laravel (Symfony Bridge):
Request/Response in Symfony’s equivalents (e.g., Symfony\Component\HttpFoundation\Request) for full profiling.use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
$symfonyRequest = SymfonyRequest::createFromGlobals();
$profiler->collect($symfonyRequest, $response);
Custom Data Collection:
collect() method in a custom service:
// src/Service/ApiProfilerCollector.php
class ApiProfilerCollector extends \ChrisJohnson00\ApiProfilerBundle\Collector\ApiProfilerCollector
{
public function collect(Request $request, Response $response, DataCollector $data)
{
parent::collect($request, $response, $data);
$data->addCustomMetric('business_logic_time', $this->measureBusinessLogic());
}
}
services.yml:
services:
api_profiler.collector:
class: Service\ApiProfilerCollector
tags:
- { name: 'api_profiler.collector' }
Conditional Profiling:
if (!$this->app->environment('local')) {
$profiler->disable();
}
Symfony Dependency:
Request/Response objects. Use a bridge (e.g., symfony/http-foundation) to convert Laravel objects:
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
$symfonyRequest = SymfonyRequest::create(
$request->path(),
$request->method(),
$request->query->all(),
$request->request->all(),
$request->server->all(),
$request->cookies->all(),
$request->files->all()
);
Deprecated Bundle:
Threshold Configuration:
config.yml:
api_profiler:
warning_threshold: 1000 # 1 second
error_threshold: 2000 # 2 seconds
Toolbar Not Showing:
profiler: { only_exceptions: false } is set in config.yml.AppKernel.php.Missing API Data:
ProfilerListener to force collection:
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
$profiler->collect($event->getRequest(), $event->getResponse());
Performance Overhead:
if (app()->environment('production')) {
$profiler->disable();
}
Custom Data Panels:
ApiProfilerCollector to add tabs (e.g., database queries, cache hits):
public function getData()
{
return array_merge(parent::getData(), [
'cache_stats' => $this->getCacheStats(),
]);
}
API-Specific Filters:
if (strpos($request->getPathInfo(), '/api/v1') === 0) {
$profiler->collect($request, $response);
}
Export Profiler Data:
$profilerData = $profiler->getData();
file_put_contents(
storage_path('logs/api_profiler.json'),
json_encode($profilerData)
);
How can I help you explore Laravel packages today?