dkx/slim-injectable-routes

This package is abandoned and no longer maintained. No replacement package was suggested.

Inject custom arguments into route callbacks

0.0.1 2019-02-03 13:39 UTC

This package is auto-updated.

Last update: 2024-02-05 01:58:32 UTC


README

Inject custom arguments into slim route callbacks

Supports only class routes!

Installation

$ composer require dkx/slim-injectable-routes

Usage

<?php

use DKX\SlimInjectableRoutes\InjectableRoutes;
use DKX\MethodInjector\MethodInjector;
use Slim\Container;

$c = new Container();
$c['foundHandler'] = function() {
    $routes = new InjectableRoutes;
    $routes->provideInjectableSetup(function(MethodInjector $injector) {
        $injector->provideValue(\stdClass::class, new \stdClass);
    });
    
    return $routes;
};

Now you will be able to automatically inject the stdClass into your route like this:

<?php

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

final class MyRoute
{

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args, \stdClass $std): ResponseInterface
    {
        // ...
        return $response;    
    }

}

To find more information about the MethodInjector see the documentation for dkx/method-injector.