2

I'm new to drupal 8. I would like to extend the user login form by deleting the username field and adding the email field. So I created a custom module, I implemented an Event Subscriber and declared it as a service :

services:
    mdmuser.route_subscriber:
        class: Drupal\mdm_user\Routing\RouteSubscriber
        tags: 
             - {name: event_subscriber }

And here's my class RouteSubscriber

 <?php

 /**
 * @file
 * Contains \Drupal\mdm_user\Routing\RouteSubscriber.
 */

 namespace Drupal\mdm_user\Routing;

 use Drupal\Core\Routing\RouteSubscriberBase;
 use Symfony\Component\Routing\RouteCollection;

 /**
 * Listens to the dynamic route events.
 */
 class RouteSubscriber extends RouteSubscriberBase {

 protected function alterRoutes(RouteCollection $collection) {

     if ($route = $collection->get('user.login')) {
      $route->setDefault('_form', '\Drupal\mdm_user\Form\UserLoginForm');
      $collection->add('user.login', $route);
    }
  }
}

I have this error when I want to install my new module :

ReflectionException: Class Drupal\mdm_user\Routing\RouteSubscriber does not exist in ReflectionClass->__construct() (line 31 of core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php).

Any idea is appreciated !

Thanks

6
  • What's the path to the file? It should be: /modules/mdm_user/src/Routing/RouteSubscriber.php – Clive Mar 17 '16 at 16:23
  • Yes, it looks good : modules/custom/mdm_user/src/Routing/RouteSubscriber.php – Debbz Mar 17 '16 at 16:29
  • Have you done a cache rebuild since adding the code? – Clive Mar 17 '16 at 16:30
  • I cleared cache again, the module is installed. But it seems my EventSubscriber is not caught. Thank you for your help ! – Debbz Mar 17 '16 at 16:43
  • There already exists a module that introduces changing the username to an email field in the login flow. so I don't recommend reinventing the wheel here. The module is called Email Registration. – Fons Vandamme Jan 10 '17 at 10:54
2

It has to be the file structure. Check again that it's at /modules/custom/mdm_user/src/Routing/RouteSubscriber.php

Also, in your class, you don't need to add the route again, it's passed by reference.

You can remove

$collection->add('user.login', $route);
0

Ran into the same issue. It ended up being stupid mistake in my namespace in my RouteSubscriper.php file. I had:

namespace Drupal\my_module\Routing\RouteSubscriber;

When it should have been:

namespace Drupal\my_module\Routing;
0

If you are getting above error message then I suggests following things to check for,

  1. Check if there are any spelling mistakes in the routing file, that is may be the name you have written is something and the file name is something else.
  2. Check the class name defined in the controller file matches the file name
  3. Folder structure is something like '\Drupal\module_name\src\Controller\exampleConfig.php' but the routing file will have something like this "\Drupal\module_name\Controller\exampleConfig" don't worry about the "src" folder
  4. Check your namespace if it exists or not and if properly written or not
  5. Check for unnecessary extra lines in *.routing.yml of your module.
  6. Add 'Controller' to end of file with your Controller Class and to end of Class Name of Controller.

I have the approach of issue with below:

Try command in bash near web/ dir of drupal 'drupal gcon', see also

Hope this helps.

Happy Coding....

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