W
W
wittyrider2015-08-12 03:33:58
symfony
wittyrider, 2015-08-12 03:33:58

What is the best way to organize work with services in the controller $this->get('service') vs controller as service?

Tired of writing code like

public function someAction() {
    $service = $this->get('app.my_super_puper_service');
}

I want something like:
/**
 * @Inject("service", "app.my_super_puper_service")
/*
public function someAction($service) {
    // do something
}

Or they will implement it using type hinting
But I have not found such a solution yet.
There is a variant controllers as services.
Questions:
1. Is this considered good practice?
2. If I use controllers as services, does it make sense to inherit from the base controller?
3. How are your controllers organized?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-12
@wittyrider

or implemented by type hinting

In fact, there are options here:
- write a compile pass that will aggregate all this information + a param converter that will inject services into the controller (you can take ready-made things as a basis, such as here and here
- use PHP-DI
This option is good only if we have thick controllers, which is bad in itself.
the controller action collects data from the request (you can just make custom requests and resolve converters through param) and transfer it to the application level service, then displays the result of its work. I use the get method and don't worry, controllers are not something to worry about at all (except that they should be thin).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question