依赖注入苗条框架 3.


Dependency Injection Slim Framework 3

我正在使用Slim Framework 3来创建API。应用结构为:MVCP(模型、视图、控制器、提供程序)。

是否可以让苗条依赖注入我的所有类?

正在使用作曲家自动加载我所有的依赖项。

我的目录结构如下所示:

/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文件。同样,依赖项由作曲家自动注入

<?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();

我的控制器看起来像这样

<?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();
    }
}

我的人员服务文件如下所示:

<?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'
        ];
    }
}

错误我现在收到以下错误:

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 容器这样做?

当您在可调用的路由中引用类时,Slim 会向 DIC 询问它。如果 DIC 没有该类名的注册,则它将实例化类本身,将容器作为唯一的参数传递给类。

因此,要为控制器注入正确的依赖项,只需创建自己的 DIC 工厂:

$container = $app->getContainer();
$container[''Controllers'PeopleController'] = function ($c) {
    $peopleService = $c->get(''Services'PeopleService');
    return new Controllers'PeopleController($c, $peopleService);
};

当然,您现在需要一个 DIC 工厂来提供人民服务:

$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 工厂。