2017-09-19 12 views
1

最新のdocsに従ってSymfony 3でサービスを書いています。symfony service declaration in services.yml

私はクラスを書いた:

<?php 

namespace julew\AdminBundle\Service; 

class FileService { 
    public function create() { 
     die('I am here!'); 
    } 
} 

マイアプリ/設定/ 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}' 

しかし、唯一の私は何を取得すると、このエラーです:

Class Symfony\Bundle\FrameworkBundle\Test\WebTestCase not found in C:\xampp\htdocs\roch\app/config\services.yml (which is being imported from "C:\xampp\htdocs\roch\app/config\config.yml").

何が間違っているのですか?

EDIT 1

問題が自動的に生成されたバンドルはそれでテストを持っていたことだった - パスを除外するためにそれらを追加する必要がありました。

しかし、今他の問題が発生しました。サービスは、コントロールのタイプヒントを介して注入されません。

Controller "julew\RochNotesBundle\Controller\ParserController::indexAction()" requires that you provide a value for the "$fileService" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

コントローラメソッドは次のようになります:

public function indexAction(FileService $fileService) { 
. 
. 
. 
} 

と私は追加トップに:

use julew\AdminBundle\Service\FileService; 
+0

src/julew/AdminBundle/bundleにテストがありますか? –

+1

除外する「テスト」を追加しますか? exclude: '../../src/julew/AdminBundle/{Test,Entity,Repository}' –

+0

ああ、それらはバンドルジェネレータから自動的に生成されましたが、今他の問題が発生しました - 編集1を参照してください – julew

答えて

2

あなたがコントローラに様々なサービスを注入したい場合は、私が手にエラーコンストラクタでこれをやや簡単に行うことができ、controller.service_argumentsタグ(services.ymlのコントローラ定義にはexplicitly addedでなければなりません)の使用を避けることができます。

+0

実際には、すでにcontroller.service_argumentsタグが追加されていますが、コンストラクタのヒントに感謝します。 – julew

関連する問題