Auto-wiring for Zend Framework router

When you use Zend Framework 2, in particular a router, you have middle-wares that handle requests. These middle-wares take as arguments the $request, the $response and (optionally) the $next middle-ware.

So, your code might look like this:

class SomePagePage
{

    /**
     * @var WidgetRenderer
     */
    private $widgetRenderer;
 
    public function __construct(
        WidgetRenderer $widgetRenderer
    )
    {
        $this->widgetRenderer = $widgetRenderer;
     }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
    {
        $someId = Guid::fromString($request->getAttribute('idContract'));
        $someIntegerArgument = (int)$request->getQueryParams()['someParameter'];
        $someFloatArgument = (float)$request->getQueryParams()['someOtherParam'];
        //.. a lot of code for parameter fetching and validation
       
        // ... some logic ...
        return new HtmlResponse($this->widgetRenderer->render('default::some/path/to/template', [
            'someData'   => $someData,
        ]));
    }

}

What if you could simplify and have all request parameter already available and of the correct type, some thing like this:

class SomePagePage
{

    /**
     * @var WidgetRenderer
     */
    private $widgetRenderer;
 
    public function __construct(
        WidgetRenderer $widgetRenderer
    )
    {
        $this->widgetRenderer = $widgetRenderer;
     }

    public function __invoke(Guid $someId, int $someInteger, float $someFloat ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
    {
        // ... ONLY the logic ...
        return new HtmlResponse($this->widgetRenderer->render('default::some/path/to/template', [
            'someData'   => $someData,
        ]));
    }

}

In this way your code would be clear of low-level casting and validation. For this, I’ve made a middle-ware that, using reflection, detects and injects the parameters in the invoke method.

All you need to do is to add it to your configuration root:

'routing' => [
    'middleware' => [
        ApplicationFactory::ROUTING_MIDDLEWARE,
        Helper\UrlHelperMiddleware::class,
        \Web\MiddleWare\AuthenticationMiddleware::class,
        ParameterInjecter::class,
        // Disable the default ZF dispatcher
        //ApplicationFactory::DISPATCH_MIDDLEWARE,
    ],
    'priority'   => 1,
],

This middle-ware works out-ot-the-box with the scalar PHP values. If you want to use custom classes for the parameters your classes must have a static method named fromString or fromPrimitive that is called by the middle-ware.

 

You can install this middle-ware using composer:

composer require xprt64/injectable-router-middleware

You can find it on github at: https://github.com/xprt64/injectable-router-middleware

Auto-wiring for Zend Framework router

Leave a Reply

Your email address will not be published. Required fields are marked *