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,
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.TimeTracker
is in your module'ssrc/Controller
directory, your namespace should beDrupal\hospitator\Controller
, notDrupal\hospitator\TimeTrackerController
.