我有一个控制器:
use Doctrine\Common\Persistence\ManagerRegistry;
class AjaxEntityController
{
protected $registry;
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
...
}这是我的services.yml:
services:
tapir_form.ajax_entity_controller:
class: Tapir\FormBundle\Controller\AjaxEntityController
arguments: [ '@doctrine' ]但是,当我试图访问这个控制器(通过它的URL)时,我会得到以下错误:
类型错误:传递给Tapir\FormBundle\Controller\AjaxEntityController::__construct()的参数1必须实现接口原则\公共\Persistence\ManagerRegistry,没有给定,在第13行的/..../var/cache/dev/jms_diextra/controller_injectors/TapirFormBundleControllerAjaxEntityController.php中调用
我搜索了这个问题,并发现很多情况下控制器没有被声明为服务或者参数丢失了,但这不是我的情况。
我清除了缓存,将@Route(service="tapir_form.ajax_entity_controller")添加到控制器类中,以防万一。我当然有我的TapirFormExtension
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');如果我从构造函数中删除强制参数,服务就能工作(我的意思是调用控制器操作,没有例外)。这是我第一次使用控制器作为服务。其他服务,如表单类型,正在使用类似的配置。
我使用的是Symfony 2.7.3,PHP 5.6.13,Linux。
还有什么不对的?
提前谢谢。
发布于 2017-05-23 16:31:27
由于Symfony 3.3,所以可以使用自动装配:
# app/services/services.yml
services:
Tapir\FormBundle\Controller\AjaxEntityController:
autowire: true或者更多的控制器,PSR-4服务自动发现
# app/services/services.yml
services:
_defaults:
autowire: true
Tapir\:
resource: src/Tapir/{Controller}https://stackoverflow.com/questions/32461740
复制相似问题