我想建立自己的__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.php 在第14行
答案 0 :(得分:0)
您应该为控制器启用autowiring
或将控制器明确配置为服务。就您而言,TaskListRepository
也应该是一项服务。