Drupal 8: Class not found from module Controller, but works fine from module EventSubscriber

by Jon Paul Vivere   Last Updated January 11, 2018 06:07 AM

I created a custom D8 module in:


In that custom module are src/Controller/MyController.php and src/EventSubscriber/MySubscriber.php.

I wrote some custom code in my custom theme in <root>/themes/mytheme/src/MyClass.php.

My namespaces are: Drupal\mymodule and Drupal\mytheme.

I can add/use MyClass in MySubscriber.php (use Drupal\mytheme\MyClass) and it works fine.

However, when trying to add MyClass to MyController.php, it creates a PHP error claiming that it cannot find MyClass. The MyClass code is identical between the two module files.

Why won't it work in MyController.php while working fine in MySubscriber.php?

I've tried a lot of testing, resetting the cache, etc., but cannot find a reason for this. I can use Drupal core classes in myController.php, but nothing from the themes directory (I tried to include custom code from another custom theme).

Any hints? Any best-practice work-arounds?

Related Questions

Caching Event Subcriber

Updated March 13, 2016 08:03 AM

How to dynamic front page in Drupal 8

Updated August 10, 2018 11:07 AM

Passing return values from controller into template

Updated March 06, 2017 20:07 PM

How to capture a Drupal 8 page as HTML?

Updated October 30, 2017 23:07 PM

Twig template hierarchy not rendering correctly

Updated February 25, 2018 21:07 PM