Custom Drupal-8 module doesn't appear

一个人想着一个人 提交于 2019-12-13 02:33:14

问题


I have successfully installed my own module on Drupal8 site. I tried to access it by entering path specified in routing.yml file but I'm getting 'Page not Found' error. I'm almost sure that my module is written correctly(I was following a tutorial where the same module where accessed successfully). What could cause my issue and how can I solve it?

here are my module files: 1)kalvis.info.yml

name: 'Kalvis'
description: 'My module'
type: 'module'
core: 8.x

2)kalvis.routing.yml

kalvis.content:
    path: /kalvis/{$from}/{$to}
    defaults:
        _content: 'Drupal\kalvis\Controller\kalvisController::content'
        _title: 'My module'
    requirements:
    _permission: 'access content'

3)kalvisController.php

<?php

namespace Drupal\kalvis\Controller;
use Drupal\Core\Controller\ControllerBase;
class kalvisController extends ControllerBase{
    public function content($to, $from)
    {
        $message = $this->t('%from sends message %to', [
            '%from' => $from,
            '%to' => $to,
        ]);
        return $message;
    }
}
?>

Here is how I'm storing those module files:

www/drupal8/modules/kalvis
                    kalvis.info.yml
                    kalvis.routing.yml
                    /src/Controller
                        kalvisController.php

I tried to access module by entering URL's like http://localhost/drupal8/admin/kalvis/Kalvis/Drupal and http://localhost/drupal8/kalvis/Kalvis/Drupal but still get the same problem.

I'm using Drupal 8.0.0 beta10 installed on localhost(WAMP)


回答1:


In the routing yml file add the single quotation marks around the value for your path. Also remove the $ sign from the 2 parameters.

Since beta 4 of Drupal 8 you have to specify the path as _controller which should return a render array.

kalvis.routing.yml file as:

kalvis.content:
  path: '/kalvis/{from}/{to}'
  defaults:
    _controller: '\Drupal\kalvis\Controller\kalvisController::content'
    _title: 'My module'
  requirements:
    _permission: 'access content'

in your kalvisController.php, change the return value to a render array return array('#markup' => $message);



来源:https://stackoverflow.com/questions/30372745/custom-drupal-8-module-doesnt-appear

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