You are here

public function UserCurrentPathsController::wildcardActionRedirect in User current paths (user/edit & user/current/*) 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/UserCurrentPathsController.php \Drupal\user_current_paths\Controller\UserCurrentPathsController::wildcardActionRedirect()

Handles wildcard (user/current/*) redirects for the current user. Replaces the second "current" parameter in the URL with the currently logged in user and redirects to the target if the resulting path is valid. Ohterwise throws a NotFoundHttpException. This is safe because the redirect is handled as if the user entered the URL manually with all security checks.

Parameters

string $wildcardaction:

Request $request:

Return value

void

1 string reference to 'UserCurrentPathsController::wildcardActionRedirect'
user_current_paths.routing.yml in ./user_current_paths.routing.yml
user_current_paths.routing.yml

File

src/Controller/UserCurrentPathsController.php, line 24

Class

UserCurrentPathsController

Namespace

Drupal\user_current_paths\Controller

Code

public function wildcardActionRedirect($wildcardaction = 'view', Request $request) {
  $currentUserId = (int) \Drupal::currentUser()
    ->id();
  $path = '/user/' . $currentUserId;
  if ($wildcardaction != 'view') {

    // /view doesn't exist for user entities
    $path .= '/' . $wildcardaction;
  }
  $url = \Drupal::service('path.validator')
    ->getUrlIfValid($path);
  if ($url !== false) {

    // Valid internal path:
    return $this
      ->redirect($url);
  }
  else {
    throw new NotFoundHttpException();
  }
}