Why is my Drupal 8 module show Page not found?

戏子无情 提交于 2019-12-25 16:00:52

问题


I'm following the Modules, Routes and Controllers tutorial. However, I kept getting "Page not found" regardless of "Clear all cache".

dino_roar.info.yml file:

name: Dino ROAR
type: module
description: "ROAR at you"
package: Custom
core: 8.x

dino_roar.routing.yml

dino_says:
  path: /the/dino/says/
  defaults:
    _controller: Drupal\dino_roar\Controller\RoarController::roar
  requirements:
    _permission: 'access content'

and src/Controller/RoarController.php file:

<?php

namespace Drupal\dino_roar\Controller;
use Symfony\Component\HttpFoundation\Response;

class RoarController
{
    public function roar(){
        return new Response('ROOOOAR');
    }
}

When accessing through: http://drupal-8.dd:8083/the/dino/says/ I get the page not found error. Any suggestion is much appreciated.

Below is my folder structure:

And this is what it looks when I collapsed the src folder.


回答1:


The main module info yml file has to be named after the following scheme:

module_name.info.yml

So it's dino_roar.info.yml for your module. Also you need to enable your module, but as far as I know when the correct named info yml file is not present Drupal doesn't even know about your module.

Also make sure your folder structure is correct:

dino_roar
  --> src
    --> Controller
           RoarController.php
  dino_roar.info.yml
  dino_roar.routing.yml



回答2:


Got some help from Shashwat Purav at Drupal.org. Below is the routing file I've changed to that works.

dino_roar.dino_says:
    path: /dino/says
    defaults:
        _controller: '\Drupal\dino_roar\Controller\RoarController::roar'
    requirements:
        _permission: 'access content'



回答3:


I think it is because of your original routing issue, you should change "path: /the/dino/says/" to "path: /the/dino/says"

after remove the last slash, should be fine .



来源:https://stackoverflow.com/questions/36457473/why-is-my-drupal-8-module-show-page-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!