我正在做一个简单的“Hello World”drupal模块,但它不起作用。
我在drupal/web/modules/custom/hello_world
模块已安装并正常工作。
现在我用这段代码添加了一个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'和/ src中的HelloWorldController.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') ]; } }当我点击“清除缓存”以便添加路由和控制器时,我得到:
The website encountered an unexpected error. Please try again later.在添加控制器和路由之前,这不会发生。
有帮助吗?
I am doing a simple "Hello World" drupal module but it doesn't work.
I have the hello_world.info.yml in drupal/web/modules/custom/hello_world
The modules is installed and working.
Now I added a hello_world.routing.yml with this code:
hello_world.hello: path: '/hello' defaults: _controller: '\Drupal\hello_world\Controller\HelloWorldController::helloWorld' _title: 'Our first route' requirements: _permission: 'access content'And a HelloWorldController.php in /src:
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') ]; } }When I click on "clear cache" so the routing and the controller are added, I get:
The website encountered an unexpected error. Please try again later.This does not happen before I added the controller and routing.
Any help?
最满意答案
将控制器放入/src/Controller而不仅仅是/src 。
并将所有出现的helloWorld()重命名为content()并继承文档,如引言Drupal 8路由和控制器示例中所示 。
Put your controller into /src/Controller not just /src.
And rename all occurrences of helloWorld() to content() and inherit docs as in Introductory Drupal 8 routes and controllers example.
更多推荐
发布评论