0

I am trying to alter the route for the login and registration pages. I followed Altering existing routes and adding new routes based on dynamic ones. I cleared the cache repeatedly, and re-enabled the module several times, but it looks like alterRoutes() is not triggered, even when I try to add a kint() debug or any code.

What I noticed is that alterRoutes() -> kint() is triggered when I use Drush to enable the module. In this case, I see kint() output in my terminal. However, alterRoutes() is not triggered when viewing the site.

This is the code I am using.

md_routes/src/Routing/RouteSubscriber.php

namespace Drupal\md_routes\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;
/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    kint($collection);
    // Change path '/user/login' to '/login'.
    if ($route = $collection->get('user.login')) {
      $route->setPath('/login');
    }
    if ($route = $collection->get('user.register')) {
      $route->setPath('/register');
    }
  }
}

md_routes/md_routes.services.yml

services:
  md_routes.route_subscriber:
    class: Drupal\md_routes\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }
  • Did define md_routes.info.yml and . module files? – awm Mar 31 '18 at 15:21
  • I did define the info.yml but not a .module file. Is this required? And what should the content be? – Skywalker Mar 31 '18 at 15:22
  • I tried adding an empty .module file. Re-enabled the module and cleared caches. But this is still not working. – Skywalker Mar 31 '18 at 15:38
  • 3
    Alter routes won't be triggered on every page request. It is built once until you rebuild the routes or clear the cache. So if the routes alteration are successful, meaning when the paths /login and /register are working as intended then all is good. – awm Mar 31 '18 at 15:38
  • I have a custom page (using the page_manager module) with a panels view. In the region i render the login block. In the login block the link that sais 'Create new account' using the user.register route still has the default path. This custom page does have /login as path and i also have on for register, does this conflict? – Skywalker Mar 31 '18 at 15:40
-1

I did clear the drupal caches using the interface (admin/config/development/performance), it seems like drush is not clearing ALL caches. It does work now and i guess it was a caching issue. Thanks anyways!

  • Drush should be clearing all caches. If it doesn't, then you might be using a different cache backend when using drush, e.g. there might be something in APCu or Redis/Memcache with a prefx that is different for web requests than with drush – Berdir Apr 1 '18 at 14:20

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.