0

If an authenticated user navigates to /login on a Drupal 8 site, she gets the "access denied" error. I want the site to redirect that user to /user (route user.page) instead. Unfortunately my RouteSubscriber doesn't work:

class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    /* @var \Symfony\Component\Routing\Route $route */
    if ($route = $collection->get('user.login')) {
      $route->setPath('/anmelden');
      $route->setRequirements([]);
      $route->setDefaults(array(
         '_controller' => '\Drupal\asdentbase\Controller\LogonController::login',
         '_title' => 'Anmelden'
       ));
    }

And my LogonController looks like

public function login() {

    if ($this->currentUser()->isAuthenticated()) {
      return $this->redirect('user.page');
    }

    $rc =
    ...

The route redirection to /anmelden works fine. But my LogonController::login function is never called for already authenticated users. There's seems to be any further magic in the core.

More detail:

I also have a route in mymodule.routing.yml like

asdentbase.login:
  path: '/anmelden'
  defaults:
    _controller: '\Drupal\asdentbase\Controller\LogonController::login'
    _title: 'Anmelden'
0

You also need to register your class in the event subscriber serivce. Create a YML similar to this:

asdentbase.services.yml

services:
  asdentbase.route_subscriber:
    class: Drupal\asdentbase\RouteSubscriber
    tags:
      - { name: event_subscriber }
  • I already did this. That's why the redirection basically works. – Rainer Feike Oct 10 at 8:30

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.