How do I call a controller function in a module? I am trying to access a static method of a controller class from a module, but my code doesn't work.

This is the controller class.

namespace Drupal\content_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;

class ContentStatisticsCount extends ControllerBase {

  public static contentStatisticsCount() {
    $value = $this-&gt;t('hi');
    return $value;
  }

}


This is the module code.

use Drupal\Core\Controller\ControllerBase;
use Drupal\content_statistics\Controller;

/**
 * Implements hook_node_view().
 */ 
function content_statistics_node_view($node, $display, $view_mode, $langcode) {
  $value = ContentStatisticsCount::contentStatisticsCount();
  print_r($value);
  die;
}


This is giving me the following PHP error:


  PHP Fatal error:  Class 'ContentStatisticsCount' not found in /home/qbuser/Projects/test/modules/custom/content_statistics/content_statistics.module on line 15
If you want to define a controller, then don't use a static method:

namespace Drupal\content_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;

class ContentStatisticsController extends ControllerBase {

  public function count() {
    $value = $this-&gt;t('hi');
    return $value;
  }

}


The use statement should import the class, not the namespace. But this is not the only problem.

If you want to use a controller class in a hook, create an instance of the controller and use the method of that instance:

use Drupal\content_statistics\Controller\ContentStatisticsController

$statistics = new ContentStatisticsController;
$value = $statistics-&gt;count();


Your class and method had the same name, I split the name in a controller class name and the method count().