Score:3

Which is better way of injecting service to controller with __construct or $instance = parent::create($container)

ru flag

I'm always using this way of injecting service to controller:

<?php    
namespace Drupal\TestModule\Controller;
    
    use Drupal\Core\Controller\ControllerBase;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    class TestModuleController extends ControllerBase {
    
      protected $testModule;
    
        public function __construct(TestModule $testModule) {
        $this->testModule = $testModule;
      }
    
      public static function create(ContainerInterface $container) {
        return new static(
          $container->get('testModule.service')
        );
      }
    
      public function testModule() {
        return [
          '#type' => 'markup',
          '#markup' => $this->testModule->getTestModule(),
        ];
      }
    }

But with drupal console I generate Controller with service (drupal gcon command) and get different code:

<?php
    namespace Drupal\TestModule\Controller;
    
    use Drupal\Core\Controller\ControllerBase;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    class TestModuleController extends ControllerBase {
    
      protected $testModule;
    
      public static function create(ContainerInterface $container) {
        $instance = parent::create($container);
        $instance->testModule = $container->get('testModule.service');
        return $instance;
      }
    
      public function testModule() {
        return [
          '#type' => 'markup',
          '#markup' => $this->testModule->getTestModule(),
        ];
      }
    }

Is this second way better than first and which should I use?

Score:3
ru flag

TL;DR: The second way is better.

The constructor method __construct() is considered internal, you can not (savely) overwrite it. E.g. the number or type of mandatory function parameters might change. Therefore the first version's code might break even within a minor update of Drupal core. Only the second version is guaranteed to work at least until the next major version release.

For more information I'd suggest reading this Change record from the Webform module.

leymannx avatar
ne flag
Ah yeah, the CR is nice. I had this issue bookmarked: https://www.drupal.org/project/webform/issues/3067546
baikho avatar
us flag
Here also an interesting read on this [Safely extending Drupal 8 plugin classes without fear of constructor changes](https://www.previousnext.com.au/blog/safely-extending-drupal-8-plugin-classes-without-fear-of-constructor-changes)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.