php - 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
解决方案
您应该autowiring
为您的控制器启用或明确将控制器配置为服务。在您的情况下TaskListRepository
也应该是一项服务。
推荐阅读
- events - 如何在主干中捕获点击事件
- react-native - react-native-device-info 无法获取唯一 ID
- c# - 为什么回发后会话变量会丢失?
- html - 特定于 HTML
- javascript - Angular 9 在每次更改时滚动到顶部
- python - Pandas 重新采样错误:resample() 得到了一个意外的关键字参数“偏移量”
- typescript - 本地打字稿错误地将数组作为 ["a", ..."b"[]] 而不是 ["a", "b"] 在 TS 操场上
- jsonschema - json模式中的参数化键(v7)
- sql - Sqlite3 - 获取给定行之后的所有行,并将 TEXT 值作为主键
- c++ - 使用基类的注入类名称进行限定的依赖名称查找