2

I'm trying to create a new module with a routing file pointing to a controller. But i get the following error in my logs

ReflectionException: Class \Drupal\glue\Controller\GlueController does not exist in ReflectionMethod->__construct() (line 128 of /home/vagrant/Projects/neut-business-website/core/lib/Drupal/Core/Entity/EntityResolverManager.php).

glue.routing.yml:

hello_world:
 path: '/hello-world'
 defaults:
  _controller: '\Drupal\glue\Controller\GlueController::helloWorldPage'
 requirements:
  _permission: 'access content'

With following controller

project_folder/modules/glue/src/Controller/GlueController

<?php
namespace Drupal\glue\Controller;


class GlueController {

  public function hellowWorldPage() {
    return [
      '#markup' => t('<p>Hello world</p>')
    ];
  }
}
Ian Droogmans
  • 65
  • 1
  • 8
  • Didn't really find the solution but it suddenly works after an hour of staring, changing chunks of code. I ended up with exactly the same code (did a full diff-check).I did a lot of cache clears but that didn't seem to work, i'm still curious... – Ian Droogmans Jul 30 '15 at 16:37

5 Answers5

3

I faced a similar error while creating my first drupal 8 module. I couldn't figure out the error in your case but would like to share the fault in my case.

Somehow, myController.php file was placed outside the

/my_module/src/Controller/

folder.

Moving the file to the proper directory solved the problem.

May be this saves somebody's time someday somewhere :)

1

This can occur due to several reasons. In my case it was due to the mismatch in the machine name. Machine name of the module must match with the namespace which we use in the controller.

namespace Drupal\glue\Controller;

Here glue must be the same as the file name glue.info.yml

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
  • This issue was driving me crazy -- all my filenames and folder structures were correct and without mistakes, and all cases matched. Except my `.info.yml` files. Even though the folder architecture and namespace were all `Module/Controller/ModuleController`, the .info.yml was still `module.info.yml`. I didn't think the `.info.yml` files would hold such power. Thanks so much, most of the other solutions were insulting lol. – Jamie Poole Aug 07 '19 at 17:28
0

You have a naming mismatch.

From your controller:

public function hellowWorldPage() {

From your route file:

 _controller: '\Drupal\glue\Controller\GlueController::helloWorldPage'

Note the method name vs. the path to call it in the _controller element in the routing file.

John Corry
  • 1,567
  • 12
  • 15
0

I faced the same problem. I triple checked that all the files and folders were correctly named and correctly placed, and checked the namespaces.

I then renamed my Controller and inserted the new name in the namespaces, routingfile etc. and then it worked. Not sure why though.

Anita Jensen
  • 91
  • 1
  • 2
-2

Yeah, I had a similar problem, and after ensuring, all the modules folder structures and naming conventions were perfect, what eventually solved it was the lack of a semicolon after the declaration of the controller class.

For some reason my IDE was not complaining about this:

class FirstController extends ControllerBase {

    //code

}; // !SEMICOLON IMPORTANT
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
sansomguy
  • 82
  • 1
  • 6