0

I have created custom module named hello my files are

hello.info.yml is

name: Hello 
type: module 
description: 'My first Drupal 8 module.' 
package: Awesome modules 
version: 1.0 
core: '8.x' 

hello.module file is

<?php 

use Drupal\Core\Routing\RouteMatchInterface; 
function hello_hello_world() {    

    return t('Hello Drupal 8 World!'); 

}

** hello.routing.yml** file is

hello.content:   
  path: '/hello'   
    defaults:     
       _controller: '\Drupal\hello\Controller\HelloController::sayhello'   
  requirements:     
    _permission: 'access content'

and last one is HelloController.php is under hello/src/Controller folder

<?php

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

/**
 *
 */
class HelloController extends ControllerBase {
    /**
     * [sayhello description]
     * @return [type] [description]
     */
    public function sayhello() {
        return array('#markup' => hello_hello_world());
    }
}

when we browse this page mywebsite.com/hello it gives a 404 error page.

Note: module is successfully enabled from admin

3
  • 1
    I think _permission: 'TRUE' is not a valid thing. You need to use a permission name, such as _permission: 'access content'
    – No Sssweat
    May 7, 2016 at 10:50
  • no its still 404 May 7, 2016 at 10:55
  • Is help.routing.yml the name of the routing file of yours? It should be hello.routing.yml May 7, 2016 at 10:59

2 Answers 2

3

The indentation of your hello.routing.yml file is wrong. It should be as the following one. (See user.routing.yml.)

user.pass:
  path: '/user/password'
  defaults:
    _form: '\Drupal\user\Form\UserPasswordForm'
    _title: 'Reset your password'
  requirements:
    _access: 'TRUE'
  options:
    _maintenance_access: TRUE

Indentation in YAML files is very important, since it changes the file content.

Notice that you can use _access: 'TRUE', when the page doesn't require a specific permission. The _maintenance_access: TRUE line is not necessary. If you use it, users have access to your route even when the site is in maintenance mode; other requirements are still respected, so you could have something similar to the following.

user.login:
  path: '/user/login'
  defaults:
    _form: '\Drupal\user\Form\UserLoginForm'
    _title: 'Log in'
  requirements:
    _user_is_logged_in: 'FALSE'
  options:
    _maintenance_access: TRUE

Before making any changes, uninstall the module, fix the file, and re-install the module.

As side note, you don't need the use Drupal\Core\Routing\RouteMatchInterface; line in your hello.module file, since you aren't using that PHP interface.

1

Couple of things which is wrong in the above code:

  • help.routing.yml should be renamed to hello.routing.yml
  • Controller folder should be under src folder. Structure should be hello/src/Controller/HelloController.php.

Hope that making these changes help you to render the page without 404 error.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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