依赖注入Slim Framework 3 [英] Dependency Injection Slim Framework 3
问题描述
我正在使用Slim Framework 3创建API。该应用程序的结构为:MVCP(模型,视图,控制器,提供程序)。
I'm using Slim Framework 3 to create an API. The app structure is: MVCP (Model, View, Controller, Providers).
是否可以让Slim Dependency注入我所有的课程?
I我正在使用作曲家自动加载我所有的依赖项。
I'm using composer to autoload all my dependencies.
我的目录结构如下:
/app
- controllers/
- Models/
- services/
index.php
/vendor
composer.json
这是我的 composer.json
文件。
{
"require": {
"slim/slim": "^3.3",
"monolog/monolog": "^1.19"
},
"autoload" : {
"psr-4" : {
"Controllers\\" : "app/controllers/",
"Services\\" : "app/services/",
"Models\\" : "app/models/"
}
}
}
这是我的 index.php
文件。同样,依赖关系由作曲家自动注入
Here's my index.php
file. Again, the dependencies are being auto injected by composer
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$container = new \Slim\Container;
$app = new \Slim\App($container);
$app->get('/test/{name}', '\Controllers\PeopleController:getEveryone');
$app->run();
我的控制器看起来像这样
My controller looks like this
<?php #controllers/PeopleController.php
namespace Controllers;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
class PeopleController
{
protected $peopleService;
protected $ci;
protected $request;
protected $response;
public function __construct(Container $ci, PeopleService $peopleService)
{
$this->peopleService = $peopleService;
$this->ci = $ci;
}
public function getEveryone($request, $response)
{
die($request->getAttribute('name'));
return $this->peopleService->getAllPeoples();
}
}
我的PeopleService文件如下:
My PeopleService file looks like this:
<?php
namespace Services;
use Model\PeopleModel;
use Model\AddressModel;
use Model\AutoModel;
class PeopleService
{
protected $peopleModel;
protected $autoModel;
protected $addressModel;
public function __construct(PeopleModel $peopleModel, AddressModel $addressModel, AutoModel $autoModel)
{
$this->addressModel = $addressModel;
$this->autoModel = $autoModel;
$this->peopleModel = $peopleModel;
}
public function getAllPeopleInfo()
{
$address = $this->addressModel->getAddress();
$auto = $this->autoModel->getAutoMake();
$person = $this->peopleModel->getPeople();
return [
$person[1], $address[1], $auto[1]
];
}
}
Models / AddressModels.php
<?php
namespace Model;
class AddressModel
{
public function __construct()
{
// do stuff
}
public function getAddress()
{
return [
1 => '123 Maple Street',
];
}
}
Models / AutoModel.php
namespace Model;
class AutoModel
{
public function __construct()
{
// do stuff
}
public function getAutoMake()
{
return [
1 => 'Honda'
];
}
}
Models / PeopleModel.php
<?php
namespace Model;
class PeopleModel
{
public function __construct()
{
// do stuff
}
public function getPeople()
{
return [
1 => 'Bob'
];
}
}
错误
我现在遇到以下错误:
ERROR I'm getting the following error now:
PHP Catchable fatal error: Argument 2 passed to Controllers\PeopleController::__construct() must be an instance of Services\PeopleService, none given, called in /var/www/vendor/slim/slim/Slim/CallableResolver.php on line 64 and defined in /var/www/app/controllers/PeopleController.php on line 21
问题
如何依赖注入所有类?有没有办法自动告诉Slim的DI容器来做到这一点?
THE QUESTION How do I dependency inject all my classes? Is there a way to automagically tell Slim's DI Container to do it?
推荐答案
当您在可调用路线中引用一个类时,Slim会询问DIC。如果DIC没有该类名称的注册,则它将实例化该类本身,并将容器作为该类的唯一参数传递。
When you reference a class in the route callable Slim will ask the DIC for it. If the DIC doesn't have a registration for that class name, then it will instantiate the class itself, passing the container as the only argument to the class.
因此,要为您的控制器注入正确的依赖项,只需创建自己的DIC工厂:
Hence, to inject the correct dependencies for your controller, you just have to create your own DIC factory:
$container = $app->getContainer();
$container['\Controllers\PeopleController'] = function ($c) {
$peopleService = $c->get('\Services\PeopleService');
return new Controllers\PeopleController($c, $peopleService);
};
当然,您现在需要PeopleService的DIC工厂:
Of course, you now need a DIC factory for the PeopleService:
$container['\Services\PeopleService'] = function ($c) {
$peopleModel = new Models\PeopleModel;
$addressModel = new Models\AddressModel;
$autoModel = new Models\AutoModel;
return new Services\PeopleService($peopleModel, $addressModel, $autoModel);
};
(如果PeopleModel,AddressModel或AutoModel具有依赖项,那么您也将为其创建DIC工厂。 )
(If PeopleModel, AddressModel, or AutoModel had dependencies, then you would create DIC factories for those too.)
这篇关于依赖注入Slim Framework 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!