I am new to Drupal. i start with working on Drupal 8. I created one custom module on Hello world.Very Basic .
Getting Below Error.
ReflectionException: Class \Drupal\helloworld\Controller\HelloWorldController does not exist in ReflectionMethod->__construct() (line 132 of core\lib\Drupal\Core\Entity\EntityResolverManager.php).

Here is my Controller file.

<?php
/**
* @file
* Contains \Drupal\helloworld\Controller\HelloWorldController.
*/
namespace Drupal\helloworld\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller routines for hello module routes.
*/
class HelloWorldController extends ControllerBase {
/**
* Return the 'Hello World' page.
*
* @return string
* A render array containing our 'Hello World' page content.
*/
public function display_text() {
$output = array();
$output['hello'] = array(
'#markup' => 'Hello World!',
);
return $output;
}
}

helloworld.routing.yml:

helloworld.hello_world:
path: '/helloworld'
defaults:
_content: '\Drupal\helloworld\Controller\HelloWorldController::display_text'
requirements:
_permission: 'view helloworld'

Comments

dhruba.jyoti.saha’s picture

@karthi, have you kept the HelloWorldController.php file inside helloworld/src/Controller folder?

Can see one extra '<?php' tag.

elhousny’s picture

Hi,
I had the same problem, do you have any suggestion.

Cheers,
Hicham

idflood’s picture

I had the same issue on a production server and it was caused by the fact that my controller folder was all lowercase but it needed to be "Controller".

David Mignot
www.idflood.com
gittip

farse’s picture

I had the same problem too and just realised that my 'src' folder was called 'scr' :/

gzveri’s picture

I had the exact same problem and renaming "controller" folder inside my module into "Controller" - has fixed it . Thank you for the tip.

ancoss’s picture

I had the same problem in Ubuntu and changing "controller" to "Controller" - fixed. Thanks.

aangel’s picture

And was missing:
use Symfony\Component\HttpFoundation\Request;

NikLP’s picture

This for me too, but the docs don't explain why it's required?

farooq.salmani’s picture

It appears that you have kept your controller in wrong folder. Make sure that your controller file path is helloworld/src/Cotroller/HelloWorldController.php.

prakash.d’s picture

perfect..thanks..

joshi.rohit100’s picture

_content: '\Drupal\helloworld\Controller\HelloWorldController::display_text'

shouldn't _content key be _controller?

stefvanlooveren’s picture

I encoutered this error because I wrote Drupal lowercase in my .routing.yml file.
So instead of _controller: \drupal\helloworld\Controller\HelloWorldController::display_text
I had to use _controller: \Drupal\helloworld\Controller\HelloWorldController::display_text

ImCrazy’s picture

I was going crazy with Drupal crashing and throwing an error drupal 8 class="placeholder">InvalidArgumentException...etc when trying to run a form. I was typing the text exactly as the example videos on youtube showed but it kept crashing.

Thanks to Stegvanlooverens comments, I checked my directory and file names, and realised that my 'src' directory had capital S (i.e. Src). Renamed it with a lowercase 's' and it worked perfectly. Days of frustration because of a uppercase S in the directory structure. Check your files and folders, and remember that YAML is case sensitive.

Vikash raj’s picture

hello guys, i made a custome module in drupal 8 for insert update and delete with drupal admin link menu now i want sorting and paging in that list so i try to use
http://stackoverflow.com/questions/40234134/how-to-use-select-query-for-...

but when i try to install this module then i m geeting error

The website encountered an unexpected error. Please try again later.

Drupal\Component\Serialization\Exception\InvalidDataTypeException: Malformed inline YAML string ('Access students list page) at line 2 (near "title: 'Access students list page"). in Drupal\Component\Serialization\YamlSymfony::decode() (line 39 of core/lib/Drupal/Component/Serialization/YamlSymfony.php).

Symfony\Component\Yaml\Inline::parseScalar(''Access students list page', NULL, Array, 0, 1, Array) (Line: 71)
Symfony\Component\Yaml\Inline::parse(''Access students list page', 1, , , Array) (Line: 506)
Symfony\Component\Yaml\Parser->parseValue(''Access students list page', 1, , , 'mapping') (Line: 233)
Symfony\Component\Yaml\Parser->parse('title: 'Access students list page', 1, , ) (Line: 225)
Symfony\Component\Yaml\Parser->parse('access students list:
title: 'Access students list page', 1, ) (Line: 36)
Drupal\Component\Serialization\YamlSymfony::decode('access students list:
title: 'Access students list page') (Line: 35)
Drupal\Component\Serialization\Yaml::decode('access students list:
title: 'Access students list page') (Line: 19)

pleease help

joshi.rohit100’s picture

@vikas - As per the the warning, looks like your yaml syntax in not correct. Either just copy this from existing one or just remove your permission yml and update routing.yml file as weel then clera cache and check.

albertomota’s picture

made the same mistake.. the sample code is missing the <?php tag at the top, updated the documentation page to include it now so hopefully others won't hit the same error..

fngatia’s picture

I remember one time faced with similar problem and found out that this error is caused due to wrong yml files naming. This is what i mean:

1. namespace is CASE SENSITIVE. Check this.
2. Your module yml names should match the Name of the module. For instant if your module is HelloWorld, your yml files should match too: HelloWorld.info.yml, HelloWorld.links.menu.yml, HelloWorld.routing.yml. This will not work: helloworld.info.yml, helloworld.links.menu.yml, helloworld.routing.yml
3. If your change the yml files, the module will be disabled. Go to your Extend and enable.
Always remember to clear cache after the changes.

ramyanarayanan’s picture

Adding to the already existing solutions, the issue with me was that the Module name and the config form name did not match. The module name in the yml file had to be matched to the form file name.

name: MyPoject Domain Field Settings
description: 'MyPoject Domain Field Settings'

routing.yml
defaults:
_form: '\Drupal\ex_domain_field_access\Form\MyPojectDomainFieldSettingsForm'

src/Form:
MyPojectDomainFieldSettingsForm.php'

marco-s’s picture

I had a similar error.

ReflectionException: Class \Drupal\xyz_account\Controller\XyzAccountController does not exist in /var/www/example.com/docroot/core/lib/Drupal/Core/Entity/EntityResolverManager.php:123

In my case I had a wrong route name in my MODULE.routing.yml.

xyz_account.routing.yml

xyz_account.login:
  path: '/login'
  ...
xyz_events.list:
  path: '/events/list'
  ...

The route name have to match the module name.

joshi.rohit100’s picture

@marco-s - route name have to match with module name  this is not required. Only requirement is that ur route name should be uniqueue and follow some naming convention. There are many reasons for this error. Like - class name/file name is not matching. Class name and file name should be same and as what defined in routing.yml. Second reason can be that ur class file is not in correct directory. Another reason can be that you have not added namespace in your class.

marco-s’s picture

@joshi.rohit100 - yes, I am sorry, I didn't give enough information in my last post. I had defined a _controller parameter for each route. That was the reason for the error (Class name/file matching etc. were correct). Therefore, I had to move the xyz_event.list route to the xyz_event.routing.yml file in my xyz_event module folder.

xyz_account.login:
  path: '/login'
  _controller: '\Drupal\xyz_account\Controller\XyzAccountController::login'
  ...
xyz_event.list:
  path: '/events/list'
  _controller: '\Drupal\xyz_event\Controller\XyzEventController::list'
  ...
jmuzz’s picture

If your controller file has a syntax error you can get this message that the class does not exist.  In my case it was a misplaced semicolon.  The error it displayed did not give any indication that there was a syntax error.  You can test your controller for syntax errors by running your controller file with CLI php.  If that gives you a syntax error it will have details you can use to fix it.  If you get a different error then it probably won't be helpful.

kufeiko’s picture

Good catch, thanks for the hint.

mithesh.kavrani’s picture

Had a syntax error of missing coma.

Syntax error can be found if you use drush to clear cache after you get this error, or use drush to enable module.

Thanks!

suresh.gju’s picture

Above comment for syntax error is the issue for me. Just comment most code and run. Once it works then uncomment function by function

jayemel’s picture

I think this is indicative of how overly complicated Drupal has become with 8. When the most basic programming lesson for Drupal 8 causes so much confusion over case and naming and directory naming -- It's just too much.

Jaypan’s picture

Well, it depends on what you consider overly complicated. If you are looking for something easier to get into on a programming level, and don't need the power of Drupal's architecture, then yes, it's overly complicated. Wordpress is a much better fit for this type of situation. If you need an Enterprise level system however, Drupal is not overly complicated, it's actually an amazingly architected system. That complexity you are facing isn't there simply to make it difficult, it's built the way it is for a reason.

jayemel’s picture

I've been working with Drupal for years on an enterprise level, and I always expect it to be sometimes an easy and rewarding experience, and often a frustrating one. My comment was hasty, and I actually think d8 is very well structured with 8. My major gripe, I guess, is that documentation for basic functionality is often poor, missing, or incomplete, which increases the complexity for veterans and beginners alike.

mangesh_sathe_pune’s picture

Am agree with @jmljunior. Drupal 8 is difficult to understand as documentation is vert very poor. There is no common pattern followed while writing its documentation, they don't even have complete documentation for running version D8 and they are planning for D9 release in 2020. Finding it difficult to grasp, but in a weeks time i will cover it up!