4

I've tried to add many services into the construct of a Controller without success.

class PersonController extends Controller
{
    public function __construct(UtilityService $Utils)
    {
        $this->util = $Utils;
    }

    public function indexAction()
    {
        ...
    }

}

what's the path I must follow?

1
  • You need define your controller PersonController as a service Commented Feb 7, 2014 at 11:24

2 Answers 2

2

You need to define your PersonController controller as a service in services.yml and inject to them UtilityService:

# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
    # ...
    person.controller.class: Acme\HelloBundle\Controller\PersonController

services:
    person.controller:
        class: "%person.controller.class%"
        arguments: ["@UtilityService"]

Where:

  1. person.controller.class is a name of your controller class
  2. UtilityService in arguments is a name of servicem which you want to inject
Sign up to request clarification or add additional context in comments.

Comments

1

as @Cerad mention in this post:

The trick is to define your controllers as services and then use the service id instead of the class name.

http://symfony.com/doc/current/cookbook/controller/service.html

3 Comments

This is not a complete answer, you can not just insert a link to documentation
Also this just mentions another answer. Not really fair to @Victor
sorry if you think otherwise but I don't see the point to duplicate the answer. Even considering this is what he needs. It's clear, quickly and good enough for it purpose

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.