最近,我嘗試使用 Drupal 9.2.8 開發一個網站。我不習慣使用 PHP,很多事情對我來說看起來很奇怪(比如為什麼在路徑中使用 \ 而不是 / ???)。無論如何,我想創建一個顯示“Hello world”的自定義頁面,所以我嘗試創建一個新模塊,但是當我嘗試訪問該頁面時找不到它。
我把我所有的代碼放在下面:
name: Hello World Module
description: Creates a page showing "Hello World".
package: Custom
type: module
core: 8.x
core_version_requirement: ^8 || ^9
hello.my_page:
path: '/hello'
defaults:
_controller: '\Drupal\hello\Controller\ExampleController::myPage'
_title: 'My first page in D9'
requirements:
_permission: 'access content'
<?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',
];
}
}
我激活了模塊index.php/admin/modules
並清除了緩存index.php/admin/config/development/performance
。我嘗試使用/hello
和訪問頁面index.php/hello
,但在這兩種情況下都顯示“找不到頁面”。
你能告訴我我做錯了什麼嗎?
我更正了一些打字錯誤,但我仍然遇到同樣的問題,我嘗試將其安裝在不同的服務器上,並且可以正常工作,這似乎是我的服務器配置的問題。
不管怎樣,這個模塊適用於 Drupal 9.2.8,也許有人可以用它作為一個簡單的例子。
謝謝你。
我認為你有一個命名空間錯誤。您的模塊名稱是“hello”,但在您的命名空間中,您使用“example”而不是“hello”。如果將 ExampleController.php 和 hello.routing.yml 上的“example”更改為“hello”,模塊將正常工作。
順便說一句:您可以在沒有自定義 PHP 的情況下使用 Drupal,但是要釋放 Drupal 的全部魔力和力量,您應該學習 OOP-PHP 和 Symfony 的基礎知識。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句