Use params in container

Hi,

I want to use parameters when I set a method in container, for exemple 3 parameters on the redirect method :

<?php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteContext;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Set redirect in Container
$container->set('redirect', function(ServerRequestInterface $request, ResponseInterface $response, string $page)
{
    $routeParser = RouteContext::fromRequest($request)->getRouteParser();
    $url = $routeParser->urlFor($page);
    return $response->withHeader('Location', $url)->withStatus(302);
});

and in my middleware or controller class I want to call container method “redirect” and pass my 3 parameters :

return $this->container->get('redirect')->($request, $response, 'login');

It’s possible to do something like this ?

Thanks

Your existing code looks good to me, and it should already work fine.

Note: The (HTTP) request is context specific, and therefore it has been removed from the DI container in Slim 4. The same applies to the request specific route parser.

Your desired syntax would be invalid code, so it also is technically not possible.

only thing you might want to look at is PHP-DI - The Dependency Injection Container for humans (the dont access the container directly part)

Thanks for your answers, I will try to put this function inside another class and not in the container