You are here

public function UserSwitchController::switchuser in User Switch 8

Switch to new user.

1 string reference to 'UserSwitchController::switchuser'
userswitch.routing.yml in ./userswitch.routing.yml
userswitch.routing.yml

File

src/Controller/UserSwitchController.php, line 93

Class

UserSwitchController
Provides route responses for the Example module.

Namespace

Drupal\userswitch\Controller

Code

public function switchuser($uid) {
  if ($this->userswitch
    ->switchToOther($uid)) {
    $message = $this
      ->t('You are now @user.', [
      '@user' => $this->currentUser
        ->getDisplayName(),
    ]);
    $this->messenger
      ->addMessage($message);
  }
  $url = Url::fromRoute('entity.user.canonical', [
    'user' => $uid,
  ])
    ->toString();
  $response = new RedirectResponse($url);
  $response
    ->send();
  return new Response();
}