【发布时间】:2016-12-03 21:00:55
【问题描述】:
我使用的是 symfony 3.1.7,我在向控制器注入服务时遇到问题,我的英语不好,我认为最好显示所有代码。
错误:
类型错误:参数 1 传递给 ...Bundle\Controller\AdminController::__construct() 必须 实现接口 Symfony\Component\DependencyInjection\ContainerInterface,没有给出, 在第 2512 行调用 /......./project/var/cache/dev/classes.php
这是我的代码:
GenericRestController
namespace MyCoreBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use MyCoreBundle\Handler\GenericRestHandlerInterface as Generic;
/** other uses like FOSRestController **/
/**
* Class GenericRestController
*/
abstract class GenericRestController extends FOSRestController
{
protected $handler;
protected $container;
public function __construct(Container $container,Generic $handler)
{
$this->container = $container;
$this->handler = $handler;
}
/** other methods like get, post, put, etc **/
}
管理员控制器
namespace ...Bundle\Controller;
use MyCoreBundle\Controller\GenericRestController;
class AdminController extends GenericRestController
{
}
已解决
解决方案是按照 Cerad 所说的那样更改路线。
官方文档
services.yml
services:
app.hello_controller:
class: AppBundle\Controller\HelloController
routing.yml
hello:
path: /hello
defaults: { _controller: app.hello_controller:indexAction }
感谢 mtaqi 和 Cerad
【问题讨论】:
-
你看到
php app/console container:debug列表显示的内容了吗? -
需要调整你的路由以使用控制器作为服务。就目前而言,控制器正在由控制器解析器使用它的类名而不是容器来实例化。仔细检查将控制器定义为服务的文档。当你让它运行时,从构造函数中移除容器并调整你的服务定义以调用 setContainer。事情会干净一些。
-
你是对的@Cerad!感谢两位的帮助!
-
请不要编辑帖子以注入响应。把你的答案写在下面并接受。否则,像我这样的人会花时间阅读您解决的问题 ;-)
标签: symfony