0

I'm new on Drupal. I try to follow a tuto in order to build my own module.

Drupal 8 (latest version) is installed on MacOS. For example, I use it through URL : http://localhost/drupal/admin/content

My module is defined like this :

  • Drupal
    • code
    • modules
      • hello_world
        • hello_world.info.yml
        • hello_world.links.menu.yml
        • hello_world.routing.yml
          • src
            • Controller
              • HelloController.php
    • ...

File content : hello_world.info.yml

name: Hello World Module
description: Creates a page showing "Hello World".
package: Custom
type: module
version: 1.0
core: 8.x

File content : hello_world.routing.yml

hello_world.hello_world:
  path: '/hello'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloController::helloWorld'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

File content : hello_world.links.menu.yml

hello_world.admin:
  title: 'Hello module settings'
  description: 'example of how to make an admin settings page link'
  parent: system.admin_config_development
  route_name: hello_world.hello_world
  weight: 100

File content : HelloController.php

<?php
/**
 * @file
 * Contains \Drupal\hello_world\Controller\HelloController.
 */

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

/**
 * Defines HelloController class.
 */
class HelloController extends ControllerBase {

  /**
   * Display the markup.
   *
   * @return array
   *   Return markup array.
   */
  public function helloWorld() {
  $output = array();

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

  }

}

Regardless "clear cache", I have a page not found !

I try the urls :

But results are the same. What should be the right url ? And why I'm getting "page not found", where is the issue ?

2
  • Have you installed the module? – Clive Feb 27 '19 at 10:12
  • yes, module is checked in /drupal/admin/modules – gduh Feb 27 '19 at 10:15
1

Creating a module does not automatically enable it.

I tried your code in a clean Drupal 8 installation and /hello show the page as expected.

Try running drush en hello_world to enable you module and you should be ok.

If that is not the case, then you can use drupal route:debug to see a complete list of your site routes, so you can know if your route is registered.

3
  • The OP said the module is selected in /admin/modules. – kiamlaluno Feb 27 '19 at 12:30
  • I copy pasted the exact OP code in web/modules/hello_world, I enabled it and /hello works as expected. It has to be some mistake in OP's pathing if the module is actually enabled. – anpel Feb 27 '19 at 12:59
  • Since I installed Drupal without using Composer, I'm not able to use drush and drupal console. So, I decided to re-install Drupal using Composer and all the required tools. Then, I installed my custom module by checking directly in admin/modules. And now it's ok. If I use drush to install/uninstall it's ok too. At the end I don't know where was the issue. I don't like not understanding what the problem was, but the second install from Composer was so different, I'm not able to find why ! – gduh Feb 27 '19 at 16:36
0

Since it looks like you're just starting out D8 development, additionally to drupal route:debug command I would install devel and admin toolbar modules that help out a lot in dev. Once devel is installed you could checkout the routing information at the taskbar and see all available paths to be sure your new module path is registered.

Another thing you might wanna take a look at is turning cache of completely on your local to see your changes immediately. Take a look at this guide for that.

2
  • No, the route name doesn't need to match the controller method used for the route output. – kiamlaluno Feb 27 '19 at 16:29
  • Just tested this you're right it doesn't, edited answer now. Fresh into D8 dev from d7, maybe all examples I came across with that approach were doing it as a best practice if not coincidence. Thanks for your correction. – seroton Feb 27 '19 at 17:20

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.