简体   繁体   English

如何在Slim v3中访问中间件类中的$ container?

[英]How to access the $container within middleware class in Slim v3?

I've been reading that in Slim v2, $app was bound to the middleware class. 我一直在读Slim v2,$ app被绑定到中间件类。 I'm finding this not to be the case in v3? 我发现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? 在中间件中访问DI容器的正确方法是什么? I'm guessing there ought to be a way? 我猜应该有办法吗?

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 LE:扩展一点初始答案,如果您将中间件作为类字符串提供,则容器将被注入构造函数中

$app->add('Auth');

or 要么

$app->add('Auth:similarToInvokeMethod')

As far as I understand the code, Slim (v3) works the following way: 据我所知,代码,Slim(v3)的工作方式如下:

  • if you pass a closure as middleware, then it calls bindTo with container as param. 如果你传递一个闭包作为中间件,那么它调用bindTo与容器作为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 如果你传递一个解析为类的类/字符串,那么Slim创建实例并将容器作为param传递给构造函数

     <?php $app->add(Auth); 
  • otherwise (eg if you add a middleware instance created earlier) then it looks like you have to take care of passing all necessary references. 否则(例如,如果你添加一个先前创建的中间件实例),那么看起来你必须要处理所有必要的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM