dtx63505 2018-04-11 07:24
浏览 24
已采纳

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

I have a drupal module called hello_world.

I have an info file in drupal/web/modules/custom/hello_world called hello_world.info.yml that contains:

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

This works perfectly ; the module is in the extension list.

Now I tried to make a help hook so I made in the same folder a hello_world.module file with this code:

<?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;
    }
}

This does not work at all. The help page is not shown.

I have also tried to make a hello world page using this controller in drupal/web/modules/custom/hello_world/src/HelloWOrldCOntroller.php:

<?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')
        ];
    }
}

And this route in drupal/web/modules/custom/hello_world called 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'

This does not work either, even after clearing the cache. As I said, the hello_world.info.yml works perfectly fine, but the help hook and the controller/routing does not. The rest of the core modules work. I am using Vagrant if it matters.

  • 写回答

1条回答 默认 最新

  • doqp87012 2018-04-11 09:11
    关注

    Try to reinstall the module and clear caches and routes should be intended properly otherwise will run into error

    hello_world.hello:
      path: '/hello'
      defaults:
        _controller:'\Drupal\hello_world\Controller\HelloWorldController::helloWorld'
        _title: 'Our first route'
      requirements:
        _permission: 'access content'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测