本文介绍了自定义Drupal-8模块没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Drupal8网站上成功安装了自己的模块。我尝试通过输入routing.yml文件中指定的路径来访问它,但出现找不到页面错误。我几乎可以确定我的模块编写正确(我正在学习一个教程,其中成功访问了相同的模块)。

I have successfully installed my own module on Drupal8 site. I tried to access it by entering path specified in routing.yml file but I'm getting 'Page not Found' error. I'm almost sure that my module is written correctly(I was following a tutorial where the same module where accessed successfully). What could cause my issue and how can I solve it?

这是我的模块文件:
1)kalvis.info.yml

here are my module files:1)kalvis.info.yml

name: 'Kalvis'
description: 'My module'
type: 'module'
core: 8.x

2)kalvis.routing.yml

2)kalvis.routing.yml

kalvis.content:
    path: /kalvis/{$from}/{$to}
    defaults:
        _content: 'Drupal\kalvis\Controller\kalvisController::content'
        _title: 'My module'
    requirements:
    _permission: 'access content'

3)kalvisController.php

3)kalvisController.php

<?php

namespace Drupal\kalvis\Controller;
use Drupal\Core\Controller\ControllerBase;
class kalvisController extends ControllerBase{
    public function content($to, $from)
    {
        $message = $this->t('%from sends message %to', [
            '%from' => $from,
            '%to' => $to,
        ]);
        return $message;
    }
}
?>

这是我存储这些模块文件的方式:

Here is how I'm storing those module files:

www/drupal8/modules/kalvis
                    kalvis.info.yml
                    kalvis.routing.yml
                    /src/Controller
                        kalvisController.php

我试图通过输入URL来访问模块,例如和,但仍然遇到相同的问题。

I tried to access module by entering URL's like http://localhost/drupal8/admin/kalvis/Kalvis/Drupal and http://localhost/drupal8/kalvis/Kalvis/Drupal but still get the same problem.

我正在使用安装在localhost(WAMP)上的Drupal 8.0.0 beta10

I'm using Drupal 8.0.0 beta10 installed on localhost(WAMP)

推荐答案

在路由yml文件中添加用单引号引起来的路径值。还要从2个参数中删除 $ 符号。

In the routing yml file add the single quotation marks around the value for your path. Also remove the $ sign from the 2 parameters.

Drupal的 beta 4 8您必须将路径指定为 _controller ,该路径应返回渲染数组。

Since beta 4 of Drupal 8 you have to specify the path as _controller which should return a render array.

kalvis.routing .yml 文件为:

kalvis.content:
  path: '/kalvis/{from}/{to}'
  defaults:
    _controller: '\Drupal\kalvis\Controller\kalvisController::content'
    _title: 'My module'
  requirements:
    _permission: 'access content'

在您的 kalvisController.php 中>,将返回值更改为渲染数组 return array('#markup'=> $ message);

in your kalvisController.php, change the return value to a render array return array('#markup' => $message);

这篇关于自定义Drupal-8模块没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 15:24