在冲突中创建服务以将依赖项注入类的问题

时间:2019-01-16 05:36:34

标签: dependency-injection slim

我正在尝试将依赖项注入Slim框架的类中。我在容器中创建了一个服务来执行此操作,但出现此错误。

PHP可捕获的致命错误:传递给Api \ Controllers \ StudentWorkPriceController :: __ construct()的参数2必须是Api \ Models \ StudentWorkPrice的实例,没有给出,在/ studentwork / vendor / slim / slim / Slim / CallableResolver中调用第93行的.php,并在第29行的/studentwork/src/api/Controllers/StudentWorkPriceController.php中定义

我的代码如下:

dependencies.php

$container = $app->getContainer();
$container['\Api\Controllers\StudentWorkPriceController']=function ($c){
    $studentWorkPrice = new \Api\Models\StudentWorkPrice();
    return new \Api\Controllers\StudentWorkPriceController($c,$studentWorkPrice);

};

index.php

use Api\Controllers\StudentWorkPriceController;
require __DIR__ . '/src/dependencies.php';

$app->get('/stuworkprice[/{params:.*}]',StudentWorkPriceController::class . ':select')->setName('StudentWorkPrice.select'); 

StudentWorkPriceController.php

use Api\Models\StudentWorkPrice as StudentWorkPrice;

class StudentWorkPriceController
{
   // protected $logger;
   // protected $pdo;
    protected $stuWorkPrice;

    /**
     * StudentWorkPriceController constructor.
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container, StudentWorkPrice $stuWorkPrice)
    {
       // $this->logger = $container->get('logger');
       // $this->pdo = $container->get('pdo');
        $this->stuWorkPrice = $stuWorkPrice;
    }

    public function select(){
    }
}

似乎我在容器中定义的服务未运行!有人可以帮我弄清楚这里发生了什么事!

非常感谢

0 个答案:

没有答案