I have a Drupal 8 module, called report. In my report.routing.yml file, I have:
report.dbtesting:
path: '/dbtesting'
defaults:
_controller: Drupal\report\Controller\dbTest::showContent
requirements:
_permission: 'access content'
In my report.services.yml, I have:
services:
drupal.testcontroller:
class: Drupal\report\Controller\dbtest
arguments: ['@database']
In my dbtest.php controller, I have:
<?php
namespace Drupal\report\controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
class dbtest extends ControllerBase {
/**
* @var \Drupal\Core\Database\Connection
*/
private $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public function showContent() {
return [
'#markup' => 'test'
];
}
}
When I go to mysite.com/dbtest, I encountered the following error:
The website encountered an unexpected error. Please try again later. ArgumentCountError: Too few arguments to function Drupal\report\controller\dbtest::__construct(), 0 passed in /mysite.com/docroot/core/lib/Drupal/Core/Controller/ControllerBase.php on line 118 and exactly 1 expected in Drupal\report\controller\dbtest->__construct() (line 16 of /mysite.com/modules/custom/report/src/Controller/dbtest.php).
I have already added an argument for the db connection and did exactly as recommended in Symfony. There is no reliable example on the Drupal site. I also checked this forum, https://www.drupal.org/forum/support/module-development-and-code-questions/2016-07-22/solved-need-some-help-with-a-simple.