I'm trying to switch user session thanks to Drupal Switch. I want to do this so that when I have a certain role, I can switch to another user account to check some data from the user i choose to switch.

My routing :

    path: '/double-vue'
        _controller: '\Drupal\double_vue\Controller\DoubleVueController::doubleVue'
        _title: 'Double vue'
        _permission: 'access content'
    path: '/double-vue/{userSwitchedID}'
        _controller: '\Drupal\double_vue\Controller\DoubleVueController::switchUser'
        _title: 'UserSwitched'
        _permission: 'access content'

My view :

<a class="btn btn-primary btn-xs" href="{{ path('double_vue.switched_user', { 'userSwitchedID': "15" }) }}">switch</a>

My function :

  public function switchUser($userSwitchedID){
    $accountSwitcher = \Drupal::service('account_switcher');
    $account = \Drupal\user\Entity\User::load($userSwitchedID);
    return $accountSwitcher->switchTo($account);

When I click on my to link to switch to another user, I have this error in my logs :

The controller must return a response (Object(Drupal\Core\Session\AccountSwitcher) given)

If I remove the return, I have this :

The controller must return a response (null given)

How can I fix that so that I can switch to another user account ?

  • You have switched to a different user account - but you also need to return a response for the request. Could be a normal page, a redirect, even a binary file download - but you'll need to return a response
    – Clive
    Commented Aug 30, 2018 at 10:50
  • So what should I do ? Commented Aug 30, 2018 at 11:42
  • Return whatever response you need to - I couldn't tell you what that is, obviously, it's your requirement :)
    – Clive
    Commented Aug 30, 2018 at 11:47
  • I don't realy know, I have a "super user" who needs to have access to other user account by my view, I understood that I need to use the switch, but I don't what does this should return Commented Aug 30, 2018 at 11:50
  • 1
    The question you've asked has been answered, you need to ask a new question for next problem. There's no way you'll get a tutorial of how to build the entire functionality of the Masquerade module in a single question, it's not how this site is structured. If you break all of your tasks down and when you get stuck at any point, ask a single, narrowly-scoped question each time, you'll be able to build your own tutorial, while respecting the ethos of the site at the same time
    – Clive
    Commented Aug 30, 2018 at 12:51

1 Answer 1


So first, the account switcher is for a temporary switch in the same request, to execute a specific thing as another user, it can't be used for what you want.

What Masquerade does is a lot more complicated and I don't understand why you don't just use that. Masquerade automatically adds itself to the user operations, so if you build a view that lists your users with the operations field then you will have exactly what you want to do.

The Masquerade module allows site administrators (or anyone with enough permissions) to switch users and surf the site as that user (no password required). That person can switch back to their own user account at any time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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