Проблема в создании службы в контактере для введения зависимости в класс - PullRequest
0 голосов
/ 16 января 2019

Я пытаюсь внедрить зависимость в класс в среде Slim.Я создал сервис в контейнере, чтобы сделать это, но я получил эту ошибку.

PHP Пойманная фатальная ошибка: Аргумент 2, передаваемый в Api \ Controllers \ StudentWorkPriceController :: __ construct (), должен быть экземпляром Api \ Models \ StudentWorkPrice, не заданным, вызываемым в / studentwork / vendor / slim / slim /Slim / CallableResolver.php в строке 93 и определен в /studentwork/src/api/Controllers/StudentWorkPriceController.php в строке 29

Мой код выглядит следующим образом:

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(){
    }
}

Кажется, что служба, которую я определил в контейнере, не работает!Может ли кто-нибудь помочь мне выяснить, что здесь происходит!

Большое спасибо

...