Drupal 挂钩和路由根本不起作用

Drupal hooks and routes not working at all

我有一个名为 hello_world 的 drupal 模块。

我在 drupal/web/modules/custom/hello_world 中有一个名为 hello_world.info.yml 的信息文件,其中包含:

name: Hello World
description: Hello World module
type: module
core: 8.x
package: Custom

这非常有效;该模块在扩展列表中。

现在我尝试创建一个帮助挂钩,所以我在同一文件夹中创建了一个 hello_world.module 文件,代码如下:

<?php

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */

function hello_world_help($route_name, RouteMatchInterface $route_match) {
    switch ($route_name) {
        case 'help.page.hello_world':
            $output = '';
            $output .= '<h3>' . t('About') . '</h3>';
            $output .= '<p>' . t('This is an example module.') . '</p>';
            return $output;
            break;
    }
}

这根本行不通。未显示帮助页面。

我也曾尝试在 drupal/web/modules/custom/hello_world/src/HelloWOrldCOntroller.php 中使用此控制器制作一个 hello world 页面:

<?php

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Controller for the salutation message.
 */
class HelloWorldController extends ControllerBase
{
    /**
     * Hello World.
     *
     * @return string
     */
    public function helloWorld()
    {
        return [
            '#markup' => $this->t('Hello World')
        ];
    }
}

drupal/web/modules/custom/hello_world 中的这条路线称为 hello_world.routing.yml:

hello_world.hello:
path: '/hello'
defaults:
_controller:
'\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
_title: 'Our first route'
requirements:
_permission: 'access content'

这也不起作用,即使在清除缓存后也是如此。正如我所说,hello_world.info.yml 工作得很好,但帮助挂钩和 controller/routing 没有。其余的核心模块工作。如果重要的话,我正在使用 Vagrant。

尝试重新安装模块并清除缓存和路由,否则会运行出错

hello_world.hello:
  path: '/hello'
  defaults:
    _controller:'\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
    _title: 'Our first route'
  requirements:
    _permission: 'access content'