简体   繁体   中英

Cannot clear cache in Drupal 8

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM