Score:0

Why am I getting "Page not found" for a simple "hello world" custom page

br flag

In my custom module, ed_helper, I have this ed_helper.routing.yml:

ed_helper.my_page:
  path: '/mypage/page'
  defaults:
    _controller: '\Drupal\ed_helper\Controller\EdHelperController::myPage'
    _title: 'My first page in D8'
  requirements:
    _permission: 'access content'

In ed_helper/src/Controller, I have this EdHelperController.php:

<?php
namespace Drupal\ed_helper\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Utility pages.
 */
class EdHelperController extends ControllerBase {

  /**
   * Returns a simple page.
   *
   * @return array
   *   A simple renderable array.
   */
  public function myPage() {
    return [
      '#markup' => 'Hello, world',
    ];
  }

}

I have confirmed that the module is installed, and I cleared the cache, but am still getting "Page not found" at /mypage/page. What am I missing?

I tried switching the theme to Claro, but I still get "page not found" at my custom route. I also tried uninstalling ed_helper and re-installing it.

My Drupal version is 10.1.

Score:0
cn flag

You can create the same simple hello world custom page with Drush:

drush generate module

Enter a module name mymodule and skip all questions except the one for a controller where you answer yes.

Install the module

drush en mymodule

and then visit www.example.com/mymodule/example to see the controller result.

br flag
This works, but I am still not able to create a custom page in my ed_helper module. Even if I copy and paste the files from the drush-generated mymodule into ed_helper and then change the namespace, filenames, etc, accordingly, it still doesn't work. There must be something wrong with my site. It may be that some of my project files aren't visible to the Docker container.
4uk4 avatar
cn flag
Don't change the namespace by hand, enter the real module name when generating the files. You can generate them in a different environment, test them and then copy the entire module directory.
br flag
The mutagen cache needed to be reset. After deleting and re-creating my Docker containers, the problem is resolved. I'm going to accept your answer because generating a module with Drush does seem like the best way to confirm that there is no issue with Drupal.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.