Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get services (dependencies) in a custom class

Tags:

In the controller, I could do

$this->get('service.name') 

But in a custom class, how can I do that?

like image 682
Jiew Meng Avatar asked Dec 24 '11 02:12

Jiew Meng


1 Answers

Define your custom class as a service, and then inject dependencies into it.

Ex:

// services.yml services:    my.custom.service.id:        class: My\Custom\Class        arguments:          - @service.name          - @doctrine.orm.entity_manager 

Your custom class' constructor would then get those services as arguments.

Be sure to read up on the Service Container in the official docs. It goes over all this in great detail.

like image 124
Steven Mercatante Avatar answered Oct 12 '22 05:10

Steven Mercatante