php - 如何在 Drupal 中显示一个简单的自定义页面?
问题描述
最近,我尝试使用 Drupal 9.2.8 开发一个网站。我不习惯使用 PHP,很多事情对我来说看起来很奇怪(比如为什么在路径中使用 \ 而不是 / ???)。无论如何,我想创建一个显示“Hello world”的自定义页面,所以我尝试制作一个新模块,但是当我尝试访问该页面时找不到它。
我把我所有的代码放在下面:
- 模块/自定义/hello/hello.info.yml
name: Hello World Module
description: Creates a page showing "Hello World".
package: Custom
type: module
core: 8.x
core_version_requirement: ^8 || ^9
- 模块/自定义/hello/hello.routing.yml
hello.my_page:
path: '/hello'
defaults:
_controller: '\Drupal\hello\Controller\ExampleController::myPage'
_title: 'My first page in D9'
requirements:
_permission: 'access content'
- modules/custom/hello/src/Controller/ExampleController.php
<?php
namespace Drupal\hello\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Provides route responses for the Example module.
*/
class ExampleController extends ControllerBase {
/**
* Returns a simple page.
*
* @return array
* A simple renderable array.
*/
public function myPage() {
return [
'#markup' => 'Hello, world',
];
}
}
我在 .in 中激活了模块index.php/admin/modules
并清除了缓存index.php/admin/config/development/performance
。我尝试使用/hello
and访问页面index.php/hello
,但在这两种情况下都显示“找不到页面”。
谁能告诉我我做错了什么?
编辑
I corrected some typing mistakes, but I still had the same problem, I tried to install it on a different server, and it works, it seems to be a problem with my server configuration.
Anyway, this module works on Drupal 9.2.8, maybe someone can use it as a simple example.
Thank you.
解决方案
I think you have a namespace bug. Your modules name is "hello", but in your namespace you use "example" instead of "hello". If you change the "example" on the ExampleController.php and hello.routing.yml to "hello", the module will work properly.
BTW: You can use Drupal without custom PHP, but to unleash the full magic and power of Drupal you should learn the basics of OOP-PHP and Symfony.
推荐阅读
- python - 我可以在我的代码中添加什么以在输出中显示排序所做的所有动作?
- r - 从 VBA 运行 R 并查看后台发生的情况
- php - 更改 WooCommerce 购物车和结帐页面中的“运输”文本:[重复新]
- javascript - 蚂蚁设计风格
- authorize.net - Authorize.net : createcustomerprofilerequest : E00116 OTS 令牌访问冲突
- graphql - 有没有办法使用 graphql 订阅将数据与聚合一起获取?
- node.js - 当 Node.js API 出现故障时,是否可以阻止 nginx 响应?
- java - 如何在没有 GMT 的情况下使用 java.time.ZonedDateTime 和 format.DateTimeFormatter?
- java - TreeSet.lower() 方法在使用显式比较器构造函数时未返回所需的输出
- postgresql - 气流 2.1.3 使用 pgbouncer 解决 postgresql 问题