Skip to main content

Using Prophiler with zend-expressive

· One min read
Stephan Hochdörfer
Head of IT Business Operations

A few weeks back we released a Prophiler PSR-7 middleware which we created during a hack session on Adroit, our little, clever ADR middleware implementation. I`d like to show you how to integrate the Prophiler middleware in a zend-expressive application. As it turns out this is super simple.

At first I had to register a pre_routing middleware in config/autoload/middleware-pipeline.local.php:

return [
'middleware_pipeline' => [[
'middleware' => bitExpert\Http\Middleware\Psr7\
Prophiler\ProphilerMiddleware::class,
'priority' => 11000,
]]
];

In addition to that, I had to add a factory definition to config/autoload/dependencies.global.php:

return [
'dependencies' => [
'factories' => [
bitExpert\Http\Middleware\Psr7\Prophiler\
ProphilerMiddleware::class =>
App\Middleware\ProphilerFactory::class
]
]
];

The \App\Middleware\ProphilerFactory implementation looks like this:

namespace App\Middleware;

use Interop\Container\ContainerInterface;
use Fabfuel\Prophiler\Profiler;
use Fabfuel\Prophiler\Toolbar;
use bitExpert\Http\Middleware\Psr7\Prophiler\ProphilerMiddleware;

class ProphilerFactory
{
public function __invoke(ContainerInterface $container)
{
$prophiler = new Profiler();
$toolbar = new Toolbar($prophiler);
return new ProphilerMiddleware($toolbar);
}
}

When running your zend-expressive application now, you should be able to the Prophiler toolbar on top of the page.

Update: Updated the code snippets as pointed out by Roma Kosolapov.