php - How to access services inside non-controller function in Symfony 2 -


you may access (let's say) doctrine service inside controller, using:

$d = $this->getdoctrine() 

now want controller call function (in class) , want class have access services. possible without passing services variable?

you have inject dependent services class or method. if thinking global object might access everywhere it's not right way go (and can't access container way). you'd miss whole point of dependency injection.

you can inject services class 2 ways:

  • manually
  • let dic it

first solution requires pass dependency either way (constructor, setter, method).

the later solution means define class service , let container construct , inject dependencies. can done if can delegate object creation dic. cannot done entities example.

be careful injecting whole container. it's not best practice. you'd introduce dependency on whole container might have different services depending on configuration. dependencies wouldn't clear.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -