-3

I tried to call a function inside other class inside the same module on D8 but I have some error on the code. I got a Error: Class 'Drupal\hospitator\TimeTrackerController\TimeTracker' not found.

My main controller (HospitatorController.php):

namespace Drupal\hospitator\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\hospitator\TimeTrackerController\TimeTracker;

/**
 * Class HospitatorController.
 */
class HospitatorController extends ControllerBase {

  /**
   * Main Page Function.
   *
   * @return array
   *   Return main page.
   */
  public function mainPage() {
    $element = array(
        '#type' => 'markup',
        '#markup' => $this->t('Welcome to Hospitator!') . '<br/>',
    );
    $test_controller = new TimeTracker;
    $test_text = $test_controller->start();

    return $element;
  }

}

My secondary class (TimeTracker.php):

<?php

namespace Drupal\hospitator\TimeTrackerController;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class TimeTracker.
 */
class TimeTracker extends ControllerBase {

  /**
   * Test Time Trakcker.
   *
   * @return string
   *   Return test text.
   */
  public function start() {
    $element = 'Test';
    return $element;
  }

}

I have been 2 years without coding and my memory have some leaks. Can you help me? Tanhk you,

3
  • 1
    The namespace used for a class must reflect the directory where the class file is located. Differently, PHP will not be able to find the class file and it will throw the error you see. As side note, a real controller is never created with new and it isn't simply a helper class you use for any purpose, but a class associated to a route defined from the module.
    – apaderno
    Apr 23, 2020 at 20:13
  • Thank you @kiamlaluno, the class will be developed for time tracking decoding qr codes synchronized with random security tokens. The TimeTracker class is located on src/Controller directory is it ok? Which namespace should I use? Thank you,
    – Alvaro dM
    Apr 23, 2020 at 20:22
  • 2
    If TimeTracker is in your module's src/Controller directory, your namespace should be Drupal\hospitator\Controller, not Drupal\hospitator\TimeTrackerController.
    – Jaypan
    Apr 23, 2020 at 21:30

1 Answer 1

1

Since Drupal 8 supports PSR-4 namespaces, as described in the Drupal coding standards, in the Namespaces document page, Modules section, there must be a correspondence between namespace and directory structure. This means that, if Drupal\hospitator\TimeTrackerController is the namespace, then the class file must be placed in the hospitator/src/TimeTrackerController folder. Furthermore, if TimeTracker is the class name, then the file containing its code must be named TimeTracker.php.
I assume that hospitator is the machine name of the module. If the machine name is different, then hospitator must be replaced from the module machine name, in the namespace and in the folder name.

If the file containing the class code is in hospitator/src/Controller/TimeTracker.php, then the correct namespace is Drupal\hospitator\Controller, not Drupal\hospitator\TimeTrackerController. When you reference that class in a file that is using another namespace, you add use Drupal\hospitator\Controller\TimeTracker; on the top of that file, not use Drupal\hospitator\TimeTrackerController\TimeTracker;.

Also, if the HospitatorController and the TimeTracker classes are using the same namespace, which I assume is Drupal\hospitator\Controller, there is no need to add use Drupal\hospitator\Controller\TimeTracker; on the top of the file containing the HospitatorController class.

1
  • Thank you @kiamlaluno, your answer is very complete. I forgot the basics of classes.
    – Alvaro dM
    Apr 25, 2020 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.