【发布时间】:2018-02-28 15:01:17
【问题描述】:
我正在根据最新的docs 在 Symfony 3 中编写服务。
我写了一个类:
<?php
namespace julew\AdminBundle\Service;
class FileService {
public function create() {
die('I am here!');
}
}
我的 app/config/services.yml 看起来像:
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
julew\AdminBundle\:
resource: '../../src/julew/AdminBundle/*'
exclude: '../../src/julew/AdminBundle/{Entity,Repository}'
但只有我得到的是这个错误:
类 Symfony\Bundle\FrameworkBundle\Test\WebTestCase 在 C:\xampp\htdocs\roch\app/config\services.yml 中找不到(正在从“C:\xampp\htdocs\roch\app/ config\config.yml")。
我做错了什么?
编辑 1
问题是,自动生成的包中包含测试 - 必须将它们添加到排除路径。
但是现在出现了其他问题。服务不是通过控制器类型提示注入的。我得到的错误:
控制器“julew\RochNotesBundle\Controller\ParserController::indexAction()”要求您为“$fileService”参数提供一个值。参数可以为空且未提供空值、未提供默认值或因为在此参数之后有一个非可选参数。
控制器方法如下:
public function indexAction(FileService $fileService) {
.
.
.
}
我在顶部添加了:
use julew\AdminBundle\Service\FileService;
【问题讨论】:
-
你的 src/julew/AdminBundle/ 包中有测试吗?
-
添加“测试”以排除?排除:'../../src/julew/AdminBundle/{Test,Entity,Repository}'
-
哦,是的,这些是从捆绑生成器自动生成的,但现在出现了其他问题 - 请参阅编辑 1
-
您是否使用 controller.service_arguments 标记您的控制器服务?您需要标签来支持将依赖项注入控制器操作。顺便说一句,从一篇文章中提出多个问题通常是个坏主意。