I have a simple question, I want Drupal to route this page but keep getting this error:
"The website encountered an unexpected error. Please try again later."
The drupal error log displays this :
"InvalidArgumentException: Class "\Drupal\dblogin\Controller\DbloginController::reddb" does not exist. in Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition() (line 24 of C:\drupal_sites\drupal-8.6.2\core\lib\Drupal\Core\DependencyInjection\ClassResolver.php)."
The code I have implemented for this is:
dblogin.reddb:
path: /admin/reddb
defaults:
_title: 'DBlogin'
_form: '\Drupal\dblogin\Controller\DbloginController::reddb'
requirements:
_permission: 'access content'
and in \\src\\Controller\\DbloginController.php
<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;
class dbloginController extends ControllerBase {
public function reddb() {
$build = [
'#markup' => $this->t('DB Login'),
];
return $build;
}
/**
* replacement process callbacks.
*/
}
?>
I have also tried to just make it return "hello", but to no avail.
<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;
class dbloginController extends ControllerBase {
public function reddb() {
return "hello";
}
/**
* replacement process callbacks.
*/
}
?>
Could anyone help me out here? Thanks in advance!
Name of class should same in routing and controller file. You should make changes in code like
<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;
class DbloginController extends ControllerBase {
public function reddb() {
return "hello";
}
/**
* replacement process callbacks.
*/
}
?>
in routing.
requirements:
_permission: 'administer site configuration'
There are two Issues with your module which I can see from code.
1) In your routing file. Please replace your code with below lines.
dblogin.reddb:
path: '/admin/reddb'
defaults:
_title: 'DBlogin'
_form: '\Drupal\dblogin\Controller\DbloginController::reddb'
requirements:
_permission: 'access content'
2) In your controller. The file name and classname of your controller should be same everytime. So you controller code would be.
<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;
class DbloginController extends ControllerBase {
public function reddb() {
$build = [
'#markup' => $this->t('DB Login'),
];
return $build;
}
/**
* replacement process callbacks.
*/
}
?>
error:
_form: '\Drupal\dblogin\Controller\DbloginController::reddb'
It must be:
_controller: '\Drupal\dblogin\Controller\DbloginController::reddb'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.