Kalvis K Kalvis K - 1 month ago 18
PHP Question

Custom Drupal-8 module doesn't appear

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)

Answer

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);

Comments