问题描述
我已经在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
我尝试通过输入 http://localhost/drupal8/admin/kalvis之类的URL来访问模块/Kalvis/Drupal 和 http://localhost/drupal8/kalvis/Kalvis/Drupal 但仍然遇到相同的问题.
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 8的 beta 4 起,您必须将路径指定为 _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模块没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!