Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject service in symfony2 Controller

How can I inject a service (the service that I created) into my Controller? A setter injection would do.

<?php
namespace MyNamespace;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    public function setMyService(MyService $myService)
    {
        $this->myService = $myService;
    }

    public function indexAction()
    {
        //Here I cannot access $this->myService;
        //Because the setter is not called magically!
    }
}

And my route settings :

// Resources/routing.yml
myController_index:
    pattern:  /test
    defaults: { _controller: "FooBarBundle:MyController:index" }

I'm setting the service in another bundle :

// Resources/services.yml 
parameters:
   my.service.class: Path\To\My\Service

services:
    my_service:
        class: %my.service.class%

When the route is resolved, the service is not injected ( I know it shouldn't ). I suppose somewhere in a yml file, I have to set:

    calls:
        - [setMyService, [@my_service]]

I am not using this Controller as a service, it's a regular Controller that serves a Request.

Edit: At this point in time, I am getting the service with $this->container->get('my_service'); But I need to inject it.

like image 490
Arsham Avatar asked Sep 03 '13 14:09

Arsham


People also ask

What is service container?

A Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (i.e. objects). For example, suppose you have a simple PHP class that delivers email messages. Without a service container, you must manually create the object whenever you need it: Copy.

What is service in Symfony?

In Symfony, these useful objects are called services and each service lives inside a very special object called the service container. The container allows you to centralize the way objects are constructed. It makes your life easier, promotes a strong architecture and is super fast!

What is dependency injection Symfony?

The Dependency Injection Container in Symfony2 allows components to be injected with their dependencies, and is often used as a Service Locator, which when combined with the DI-container pattern is considered to be an anti-pattern by many.

What is dependency injection framework?

Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern. A dependency is an object that another object depends on. Examine the following MessageWriter class with a Write method that other classes depend on: C# Copy.


2 Answers

If you want to inject services into your controllers, you have to define controllers as services.

You could also take a look at JMSDiExtraBundle's special handling of controllers — if that solves your problem. But since I define my controllers as services, I haven't tried that.

like image 132
Elnur Abdurrakhimov Avatar answered Sep 21 '22 20:09

Elnur Abdurrakhimov


When using the JMSDiExtraBundle you DON'T have to define your controller as a service (unlike @elnur said) and the code would be:

<?php

namespace MyNamespace;

use JMS\DiExtraBundle\Annotation as DI;
use Path\To\My\Service;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    /**
     * @var $myService Service
     *
     * @DI\Inject("my_service")
     */
    protected $myService;

    public function indexAction()
    {
        // $this->myService->method();
    }
}

I find this approach very nice because you avoid writing a __construct() method.

like image 21
COil Avatar answered Sep 21 '22 20:09

COil