找不到Drupal 8模块路由页面?

时间:2018-12-04 10:46:17

标签: php drupal drupal-modules drupal-8

我有一个简单的问题,我希望Drupal路由此页面,但始终出现此错误:

"The website encountered an unexpected error. Please try again later."

drupal错误日志显示以下内容:

"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)."

为此我实现的代码是:

dblogin.reddb:
  path: /admin/reddb
  defaults:
    _title: 'DBlogin'
    _form: '\Drupal\dblogin\Controller\DbloginController::reddb'
  requirements:
    _permission: 'access content'

和\ 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.
 */
}
?>

我也试图使其返回“ hello”,但无济于事。

<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;

class dbloginController extends ControllerBase {

    public function reddb() {
        return "hello";
    }
    /**
       * replacement process callbacks.
    */
}
?>

有人可以帮我吗?预先感谢!

3 个答案:

答案 0 :(得分:0)

类名称在路由和控制器文件中应该相同。您应该对代码进行更改,例如

<?php
namespace Drupal\dblogin\Controller;
use Drupal\Core\Controller\ControllerBase;

class DbloginController extends ControllerBase {

    public function reddb() {
        return "hello";
    }
    /**
       * replacement process callbacks.
    */
}
?>

在路由中。

  requirements:
    _permission: 'administer site configuration'

答案 1 :(得分:0)

您的模块有两个问题,我可以从代码中看到。

1)在您的路由文件中。请用下面几行替换您的代码。

dblogin.reddb:
  path: '/admin/reddb'
  defaults:
    _title: 'DBlogin'
    _form: '\Drupal\dblogin\Controller\DbloginController::reddb'
  requirements:
    _permission: 'access content'

2)在您的控制器中。控制器的文件名和类名每次都应该相同。这样您的控制器代码就可以了。

<?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.
 */
}
?>

答案 2 :(得分:0)

错误:_form:'\ Drupal \ dblogin \ Controller \ DbloginController :: reddb'

它必须是:

_controller:'\ Drupal \ dblogin \ Controller \ DbloginController :: reddb'