使用 Slim 3 在 class 中获取依赖注入
Get Dependency Injection in a class with Slim 3
我有 DI 的问题,这对我来说是新的。我想在我的项目 class 中将容器作为 DI,但出现错误:
Argument 1 passed to Project::__construct() must be an instance of Slim\Container, none given
我创建了一个 class 项目:
use Slim\Container;
class Project {
protected $ci;
public function __construct(Container $ci) {
$this->ci = $ci;
}
}
这是我的 DIC 配置:
//dependencies.php
$container = $app->getContainer();
$container[Project::class] = function ($c) {
return new Project($c);
};
这里是我的代码 index.php 到 运行 代码
// Instantiate the app
$settings = require __DIR__ . '/../app/configs/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../app/configs/dependencies.php';
// Register middleware
require __DIR__ . '/../app/configs/middleware.php';
// Register routes
require __DIR__ . '/../app/configs/routes.php';
require __DIR__ . '/../app/model/Project.php';
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = new Project();
return $response;
});
$app->run();
我不知道我在哪里失败了,我什至尝试使用 slim-brige,但我得到了相同的结果。我还尝试提供一个字符串而不是容器,但我仍然得到 null
Argument 1 passed to Project::__construct() must be an instance of Slim\Container, none given
好吧,确实没有给构造函数任何参数:
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = new Project(); // You should pass container instance here, but you don't
return $response;
});
由于您已经在容器中注册了Project
class,您可以从容器中获取实例:
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = $this->container->get('Project'); // Get Project instance from the container, you have already registered it
return $response;
});
我有 DI 的问题,这对我来说是新的。我想在我的项目 class 中将容器作为 DI,但出现错误:
Argument 1 passed to Project::__construct() must be an instance of Slim\Container, none given
我创建了一个 class 项目:
use Slim\Container;
class Project {
protected $ci;
public function __construct(Container $ci) {
$this->ci = $ci;
}
}
这是我的 DIC 配置:
//dependencies.php
$container = $app->getContainer();
$container[Project::class] = function ($c) {
return new Project($c);
};
这里是我的代码 index.php 到 运行 代码
// Instantiate the app
$settings = require __DIR__ . '/../app/configs/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../app/configs/dependencies.php';
// Register middleware
require __DIR__ . '/../app/configs/middleware.php';
// Register routes
require __DIR__ . '/../app/configs/routes.php';
require __DIR__ . '/../app/model/Project.php';
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = new Project();
return $response;
});
$app->run();
我不知道我在哪里失败了,我什至尝试使用 slim-brige,但我得到了相同的结果。我还尝试提供一个字符串而不是容器,但我仍然得到 null
Argument 1 passed to Project::__construct() must be an instance of Slim\Container, none given
好吧,确实没有给构造函数任何参数:
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = new Project(); // You should pass container instance here, but you don't
return $response;
});
由于您已经在容器中注册了Project
class,您可以从容器中获取实例:
$app->get('/project/{id}', function (Request $request, Response $response) {
$project = $this->container->get('Project'); // Get Project instance from the container, you have already registered it
return $response;
});