首页 > 解决方案 > Construct() 必须是 symfony 3.4 中的一个实例

问题描述

我想建立自己__construct的呼叫我的TaskListRepository,但它不工作:

<?php

namespace App\Controller;

use Doctrine\ORM\Mapping as ORM;
use App\Repository\TaskListRepository;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\AbstractFOSRestController;

class ListController extends AbstractFOSRestController
{

    /**
     * @var string $taskListRepository
     */
    private $taskListRepository;

    public function __construct(TaskListRepository $taskListRepository)
    {
        $this->taskListRepository = $taskListRepository;
    }

    public function getListsAction()
    {
        return $this->taskListRepository->findAll();
    }
}
<?php

namespace App\Repository;

use App\Entity\TaskList;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method TaskList|null find($id, $lockMode = null, $lockVersion = null)
 * @method TaskList|null findOneBy(array $criteria, array $orderBy = null)
 * @method TaskList[]    findAll()
 * @method TaskList[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class TaskListRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, TaskList::class);
    }  
    //commented lines removed for readability  
}

错误:

类型错误:传递给 App\Controller\ListController::__construct() 的参数 1 必须是 App\Repository\TaskListRepository 的实例,没有给出,在 /data/applis/GIE/var/cache/dev/ContainerAfvn1yx/getListControllerService 中调用。第 14 行的 php

标签: phpsymfonyobject

解决方案


您应该autowiring为您的控制器启用或明确将控制器配置为服务。在您的情况下TaskListRepository也应该是一项服务。


推荐阅读