In the controller, I could do
$this->get(\'service.name\')
But in a custom class, how can I do that?
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.