17

I've been reading that in Slim v2, $app was bound to the middleware class. I'm finding this not to be the case in v3? Below is my middleware class, but I'm just getting undefined:

<?php
namespace CrSrc\Middleware;

class Auth
{
    /**
     * Example middleware invokable class
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param  callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke($request, $response, $next)
    {
        // before

var_dump($this->getContainer()); // method undefined
var_dump($this->auth); exit; // method undefined
        if (! $this->get('auth')->isAuthenticated()) {
            // Not authenticated and must be authenticated to access this resource
            return $response->withStatus(401);
        }

        // pass onto the next callable
        $response = $next($request, $response);

        // after


        return $response;
    }
}

What's the correct way to access the DI container within middleware? I'm guessing there ought to be a way?

Martyn
  • 4,941
  • 9
  • 40
  • 97

2 Answers2

31

A bit late to the party but might help others... You have to inject your container when instantiating the middleware

$container = $app->getContainer();
$app->add(new Auth($container));

And your middleware needs a constructor

<?php
namespace CrSrc\Middleware;

class Auth
{

    private $container;

    public function __construct($container) {
        $this->container = $container
    }

    /**
     * Example middleware invokable class
     *
     * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
     * @param  callable                                 $next     Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke($request, $response, $next)
    {
        // $this->container has the DI

    }
}

LE: Expanding a bit the initial answer, the container gets injected in the constructor if you supply the middleware as a class string

$app->add('Auth');

or

$app->add('Auth:similarToInvokeMethod')
the-noob
  • 1,264
  • 2
  • 14
  • 19
  • Thanks. I think in the end I just assumed this was the way to go in v3. Seems to work well anyway. – Martyn Feb 18 '16 at 02:57
  • Great answer. A lot of people using Slimphp are looking for this answer but it is hidden behind the bushes known as Dependency Injection. Thanks! – user2800382 Jan 15 '18 at 23:01
-1

As far as I understand the code, Slim (v3) works the following way:

  • if you pass a closure as middleware, then it calls bindTo with container as param.
  • if you pass a class/string that resolves to a class, then Slim creates the instance and passes the container as param to the constructor

    <?php
    $app->add(Auth);
    
  • otherwise (e.g. if you add a middleware instance created earlier) then it looks like you have to take care of passing all necessary references.

Rafael
  • 17,823
  • 5
  • 52
  • 65
  • This is partially correct: `bindTo` applies Closures when passed as middleware but if you pass a class it won't supply anything in the constructor – the-noob Feb 18 '16 at 02:28
  • @the-noob I just tested it with Slim 3.0.0 and adding middleware using `$app->add(ClassName)` created instance of `ClassName` supplying container as first param to the constructor. – Rafael Feb 18 '16 at 06:39
  • that happens only if you supply the callable middleware as a string. It will go to [CallableResolver::resolve](https://github.com/slimphp/Slim/blob/3.x/Slim/CallableResolver.php#L51-L79) and that method will instantiate your class. I guess the wrong statement is `class/string` ... it is really just `string`. – the-noob Feb 18 '16 at 10:58
  • For the test mentioned in previous comment I passed class name directly and not as a string. The thing is, calling `$app->add(ClassName)` PHP treats the param as string, because classes in PHP are not "first-class citizens". So it goes through the string path inside the `CallableResolver::resolve`. – Rafael Feb 18 '16 at 11:16