joel
joel

Reputation: 19

How to access container from Service in Symfony?

I've created a service and want to access the container from within so I can access the routing, Should I inject the container or handle this in the controller which calls the service?

Upvotes: 0

Views: 629

Answers (1)

offwhite
offwhite

Reputation: 552

It's generally agreed that a bad idea to inject the container into anything.

when you declare your service in services.yml (or services.xml) you can inject other services into it:

your.awesome.service:
    class: Hippies\FlowerBundle\Service\Awesome
    arguments:
        - '@router'

and handle that int he constructor of your service class:

public function __construct($router)
{
    $this->router = $router;
}

Upvotes: 5

Related Questions