Reputation: 19
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
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