Timeline for What is the right way of loading a service?
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 16, 2020 at 14:05 | vote | accept | Marwen Amri | ||
Jan 16, 2020 at 10:57 | comment | added | 4uk4 | No, the linked blog post is not about writing a new controller or plugin by extending a base class in your own module, it's about extending existing core/contrib controllers and plugins. | |
Jan 16, 2020 at 10:35 | comment | added | Hudri |
For my own understanding: If I'm writing my own standalone class -> __construct(Services...) OK. As soon as I'm extending from another class like e.g. ControllerBase or PluginBase -> better leave the constructor alone and $container->get() the service inside create() ?
|
|
Jan 16, 2020 at 10:14 | comment | added | 4uk4 | @Hudri, this is a good point, although it's not that you shouldn't use the design pattern anymore, it's about how you can extend existing controllers which are built with this design pattern. | |
Jan 16, 2020 at 9:47 | comment | added | Hudri | Is the linked question 226997 still a recommended pattern? According to this blog post the constructor should be considered internal and subject to changes. | |
Jan 16, 2020 at 9:08 | history | edited | 4uk4 | CC BY-SA 4.0 |
deleted 1 character in body
|
Jan 16, 2020 at 9:07 | comment | added | d70rr3s |
Also worth metion that you can inject dependencies (services or parameters) to a class that is not a service and will be used on procedural code like hooks by making your class implement \Drupal\Core\DependencyInjection\ContainerInjectionInterface and by calling it in your hook using $instance = \Drupal::classResolver('\Drupal\my_module\MyAwesomeClass'); . Is not a service but will use services though.
|
|
Jan 16, 2020 at 8:56 | history | answered | 4uk4 | CC BY-SA 4.0 |