0

I keep having a 404 error no matter what I change on my routing file.

I've created a module in modules\custom which have :

  • An info file in modules\custom\first_module\first_module.info.yml:
name: First module
core: 8.x
type: module
package: Training modules
description: First Module created to learn coding in D8
  • A Routing file in modules\custom\first_module\first_module.routing.yml:
first_module_hello_world:
  path: '/hello-world'
  defaults: 
    _controller: '\Drupal\first_module\src\Controller\TrainingModule:helloWorld'
  requirements:
    _permission: 'access content'
  • A controller in modules\custom\first_module\first_module.routing.yml:
<?php
/**
 * @file
 * Contains \Drupal\hello_world\Controller\HelloWorldController.
 */

namespace Drupal\first_module\src\Controller;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for hello_world module routes.
 */
class TrainingModuleController extends ControllerBase {

 /**
  * Return the 'Hello World' page.
  *
  * @return string
  *   A render array containing our 'Hello World' page content.
  */
 public function helloWorld() {
  $output = array();

  $output['hello_world'] = array(
    '#markup' => $this->t('Hello World!'),
  );
  return $output;
 }
}

Any help would be greatly appreciated !

Edit: The Module is activated and I cleared the cache several times.

0

In first_module.routing.yml, controller there is no src and you use 2: ::

first_module_hello_world:
    path: '/hello-world'
    defaults: 
        _controller: '\Drupal\first_module\Controller\TrainingModule::helloWorld'
        _title: 'hello world'
    requirements:
        _permission: 'access content'

Also without src in drupal 8 there si no need to use return $output; Alsi class is not good. Class is TrainingModule not TrainingModuleControlle and file need to cole the some TrainingModule

namespace Drupal\first_module\Controller;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for hello_world module routes.
 */
class TrainingModule extends ControllerBase {

 /**
  * Return the 'Hello World' page.
  *
  * @return string
  *   A render array containing our 'Hello World' page content.
  */
 public function helloWorld() {

  return array(
    '#type' => 'markup',
        '#markup' => $this->t('Hello World!'),
  );
 }
}

change first_module_hello_world: to first_module_hello_world.help:

make the file first_module_hello_world.module and write this code

function first_module_hello_world_help($route_name, $route_match) {
 switch ($route_name) {
  case 'first_module_hello_world.help':
    $output = '';
    return $output;
 }
}
  • Still Page not found. I guess the first thing to check is the routing file. There must be a problem there but I can't figure out which one ! otherwhise there would have been another kind of error report. – Ismail H Jan 16 '16 at 15:57
  • A controller in need to be in modules\custom\first_module\src\Controller\TrainingModule.php did you chance also this class TrainingModule extends ControllerBase – SakaSerbia Jan 16 '16 at 16:00
  • Yes I copied your code in each file. – Ismail H Jan 16 '16 at 16:02
  • Worked with the class after rebuilding the cache ! thanks @s-a-k-a ! – Ismail H Jan 16 '16 at 16:06
  • 1
    I'm glad that I helped.. – SakaSerbia Jan 16 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.