Slim php-从中间件访问容器
我正试图从我的中间件访问Slim php-从中间件访问容器,php,slim,Php,Slim,我正试图从我的中间件访问$container,但运气不太好 在我的index.php文件中 require '../../vendor/autoload.php'; include '../bootstrap.php'; use somename\Middleware\Authentication as Authentication; $app = new \Slim\App(); $container = $app->getContainer(); $app->add(new
$container
,但运气不太好
在我的index.php
文件中
require '../../vendor/autoload.php';
include '../bootstrap.php';
use somename\Middleware\Authentication as Authentication;
$app = new \Slim\App();
$container = $app->getContainer();
$app->add(new Authentication());
然后我有一个类Authentication.php
,如下所示
namespace somename\Middleware;
class Authentication {
public function __invoke($request, $response, $next) {
$this->logger->addInfo('Hi from Authentication middleware');
但我犯了个错误
未定义的属性:somename\Middleware\Authentication::$logger in***
我也尝试过向类中添加以下构造函数,但也没有得到任何乐趣
private $container;
public function __construct($container) {
$this->container = $container;
}
有人能帮忙吗?中间件实现的最佳实践如下: 将此代码放在依赖项部分中:
$app = new \Slim\App();
$container = $app->getContainer();
/** Container will be passed to your function automatically **/
$container['MyAuthenticator'] = function($c) {
return new somename\Middleware\Authentication($c);
};
然后在身份验证类中创建构造函数,如您所述:
名称空间somename\Middleware
class Authentication {
protected $container;
public function __invoke($request, $response, $next)
{
$this->container->logger->addInfo('Hi from Authentication middleware');
}
public function __construct($container) {
$this->container = $container;
}
/** Optional : Add __get magic method to easily use container
dependencies
without using the container name in code
so this code :
$this->container->logger->addInfo('Hi from Authentication middleware');
will be this :
$this->logger->addInfo('Hi from Authentication middleware');
**/
public function __get($property)
{
if ($this->container->{$property}) {
return $this->container->{$property};
}
}
}
在index.php中添加中间件后,使用如下名称解析:
$app->add('MyAuthenticator');
中间件实现的最佳实践如下: 将此代码放在依赖项部分中:
$app = new \Slim\App();
$container = $app->getContainer();
/** Container will be passed to your function automatically **/
$container['MyAuthenticator'] = function($c) {
return new somename\Middleware\Authentication($c);
};
然后在身份验证类中创建构造函数,如您所述:
名称空间somename\Middleware
class Authentication {
protected $container;
public function __invoke($request, $response, $next)
{
$this->container->logger->addInfo('Hi from Authentication middleware');
}
public function __construct($container) {
$this->container = $container;
}
/** Optional : Add __get magic method to easily use container
dependencies
without using the container name in code
so this code :
$this->container->logger->addInfo('Hi from Authentication middleware');
will be this :
$this->logger->addInfo('Hi from Authentication middleware');
**/
public function __get($property)
{
if ($this->container->{$property}) {
return $this->container->{$property};
}
}
}
在index.php中添加中间件后,使用如下名称解析:
$app->add('MyAuthenticator');
我不同意。当添加这个PHP(\uu get
)时,代码将更难测试
应在构造函数上指定所有必需的依赖项。
好处是,您可以很容易地看到类具有哪些依赖关系,因此只需要在单元测试中模拟这些类,否则您就必须在每个测试中创建一个容器。也
我将在记录器示例中显示:
class Authentication {
private $logger;
public function __construct($logger) {
$this->logger = $logger;
}
public function __invoke($request, $response, $next) {
$this->logger->addInfo('Hi from Authentication middleware');
}
}
然后将带有logger参数的中间件添加到容器中:
$app = new \Slim\App();
$container = $app->getContainer();
$container['MyAuthenticator'] = function($c) {
return new somename\Middleware\Authentication($c['logger']);
};
注意:上述容器注册可以通过使用自动完成(但速度也应该较慢)。我不同意。当添加这个PHP(\uu get
)时,代码将更难测试
应在构造函数上指定所有必需的依赖项。
好处是,您可以很容易地看到类具有哪些依赖关系,因此只需要在单元测试中模拟这些类,否则您就必须在每个测试中创建一个容器。也
我将在记录器示例中显示:
class Authentication {
private $logger;
public function __construct($logger) {
$this->logger = $logger;
}
public function __invoke($request, $response, $next) {
$this->logger->addInfo('Hi from Authentication middleware');
}
}
然后将带有logger参数的中间件添加到容器中:
$app = new \Slim\App();
$container = $app->getContainer();
$container['MyAuthenticator'] = function($c) {
return new somename\Middleware\Authentication($c['logger']);
};
注意:使用可以自动完成容器的上述注册(但速度也应该较慢)。是否有帮助?请确保在中间件方法中正确引用$container:
$this->$container['logger']->addInfo(…)
是否有帮助?请确保在中间件方法中正确引用$container:$this->$container['logger']->addInfo(…)