2

Drupal 8 installed in Debian VPS server. Some installed themes do not work, some do. When tried to creat a simple 'Hello World' module,as described in a beginners book for drupal, installing of the module was fine; but when trying to open : www.my***site.com/hello/world i got "Access denied" message. After installing drupal i treated permissions using this and its comments through the command : chown -R www-data:www-data drupal/

The hello World module creation: in drupal/module created custom/ and contrib/ folder as well as custom/hello_world/. Within hello_world/ created three files hello_world.info.yml, hello_worl.module, hello_world.routing.ymland hello_world/src/Controller/HelloWorldController.php

hello_world.info.yml

name: Hello World
type: module
description: 'A basic Drupal 8 Hello World Module.'
package: Custom Modules
version: 1.0
core: 8.x

hello_world.routing.yml

hello_world_settings:
  path: '/hello/world'
defaults:
  _controller: '\Drupal\hello_world\Controller\HelloWorldController::
myCallbackMethod'
  _title: 'Hello World'
requirements:
  _permission: 'access content'

hello_world/src/Controller/HelloWorldController.php

<?php
/**
* @file
* Contains \Drupal\hello_world\HelloWorldController.
*/
namespace Drupal\hello_world\Controller;
/**
 * Provides route responses for the hello world page example.
 */
class HelloWorldController {
  /**
  * Returns a simple hello world page.
  *
  * @return array
  * A very simple renderable array is returned.
  */
  public function myCallbackMethod() {
    $element = array(
      '#markup' => '<p><b>Saying Hello World in Drupal 8 is cool!</b></p>',
    );
    return $element;
  }
}

upon opening www.my****website.com/admin/hello/world i got Access Denied. Looking further into My**website.com/admin/reports/dblog/event/***5 i got just that it is an Access Denied and Warning in the security field. Can anyone please explain what is going on?

  • Trying to access the page as an Admin? – Nitesh Sethia May 21 '16 at 19:16
  • Either way it does not work. Access denied for both! – pigeon May 21 '16 at 20:07
9

The issue is with the indentation of the hello_world.routing.yml file. Simply change the indentation of it to:

hello_world_settings:
  path: '/hello/world'
  defaults:
    _controller: '\Drupal\hello_world\Controller\HelloWorldController::myCallbackMethod'
    _title: 'Hello World'
  requirements:
    _permission: 'access content'

And the Access denied issue should be solved. The indentation of YAML file is important, in Drupal 8. It should be as follow:

  • path, defaults, and requirements should be indented by 2 spaces
  • _controller, _title, and _permission should be indented by 4 spaces
  • This saved more wasted hours. Should've searched for this sooner. – sam452 Oct 25 '17 at 15:54
  • in PHPStorm symfony plugin could warn you about proper yml indentation. – ssibal May 8 at 11:38

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.