Skip to main content
added 607 characters in body
Source Link
4uk4
  • 101.8k
  • 7
  • 173
  • 217

Controllers are usually notIf you want to define a controller, then don't use a static methodsmethod:

namespace Drupal\content_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;

class ContentStatisticsController extends ControllerBase {

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

}

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

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

use Drupal\content_statistics\Controller\ContentStatisticsController

$statistics = new ContentStatistics;ContentStatisticsController;
$value = $statistics->Count>count();

Your class and method havehad the same name, I made themsplit the name in a bit shortercontroller class name and the method count().

Controllers are usually not static methods. Create an instance of the object and use the method of that instance:

$statistics = new ContentStatistics;
$value = $statistics->Count();

Your class and method have the same name, I made them a bit shorter.

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->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->count();

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

deleted 30 characters in body
Source Link
4uk4
  • 101.8k
  • 7
  • 173
  • 217

Controllers are usually not static methods. Create an instance of the object and use the method of that instance:

$statistics = new ContentStatisticsCountClass;ContentStatistics;
$value = $statistics->contentStatisticsCount>Count();

Your class and method have the same name, I addedmade them a Class to the firstbit shorter.

Controllers are usually not static methods. Create an instance of the object and use the method of that instance:

$statistics = new ContentStatisticsCountClass;
$value = $statistics->contentStatisticsCount();

Your class and method have the same name, I added a Class to the first.

Controllers are usually not static methods. Create an instance of the object and use the method of that instance:

$statistics = new ContentStatistics;
$value = $statistics->Count();

Your class and method have the same name, I made them a bit shorter.

Source Link
4uk4
  • 101.8k
  • 7
  • 173
  • 217

Controllers are usually not static methods. Create an instance of the object and use the method of that instance:

$statistics = new ContentStatisticsCountClass;
$value = $statistics->contentStatisticsCount();

Your class and method have the same name, I added a Class to the first.