1
votes

I am writing a service in Symfony 3 according to latest docs.

I wrote a class:

<?php

namespace julew\AdminBundle\Service;

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

My app/config/services.yml looks like:

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}'

But only what i get is this error:

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").

What am i doing wrong?

EDIT 1

The problem was, that automatically generated bundles had tests in it - had to add them to exclude paths.

But now other problem occured. Service is not injected via controler type-hint. Error that i get:

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.

The controller method looks like:

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

And at the top i added:

use julew\AdminBundle\Service\FileService;
1
Do you have tests in your src/julew/AdminBundle/ bundle? - Carl Boisvert
Add "Tests" to exclude? exclude: '../../src/julew/AdminBundle/{Test,Entity,Repository}' - Alex.Barylski
Oh yes, those were generated automatically from the bundle generator, but now other problem occured - see Edit 1 - julew
Are you tagging your controller service with controller.service_arguments? You need the tag to support injecting dependencies into controller actions. BTW, it is typically a bad idea to ask multiple questions from one post. - Cerad

1 Answers

2
votes

If you want to inject various services into a controller, it can be a little easier to do so in the constructor, and avoid the use of the controller.service_arguments tag (which has to be explicitly added for the controller definition in the services.yml).