Using parameters in routes I am using the pathauto module to create url aliases for all user account url, so:

/user/[uid] will become /u/[user:account-name]-[user:uid]

which means:

/user/36 will become /u/john-smith-36

In my custom module, I have created a route to fire a controller when /u/john-smith-36/settoken?token=123456 url is accessed.

The controller will:


Fetch the account of the current logged in user.
Update the text field field_user_push_token with the query parameter value:
123456
Save the account of the current logged in user.
Redirect the user to the page: /thank-you


I am finding a difficulty on how to use parameters in route for the user account url !

save_push_token.routing

save_push_token.SavePushToken:
  path: '/{user}/settoken'
  defaults:
    _controller: '\Drupal\save_push_token\Controller\SavePushTokenController::SavePushToken'
  requirements:
    _permission: 'access content'


SavePushTokenController.php

&lt;?php

namespace Drupal\save_push_token\Controller;

use Symfony\Component\HttpFoundation;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SavePushTokenController extends ControllerBase {

  /**
   * Display the markup.
   *
   * @return array
   */
  public function SavePushToken(AccountInterface $user, Request $request) {

// Get token value from url.
    $request = Request::createFromGlobals();
    $userpushtoken = $request-&gt;query-&gt;get('token');

// Load the (current logged user) account.
// No need to load the user account because of {user}
    // $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());

// Set the (token).
    $user-&gt;set('field_user_push_token', $userpushtoken);

// Save the (current logged user) account.
    $user-&gt;save();

// Redirect to "/thank-you" page.
    return new RedirectResponse('/thank-you');

  }
}


But simply the controller is not fired when I access for example /u/john-smith-36/settoken?token=123456 url is accessed.

any help on how to properly use parameters in routes ?
I see two problems. First your routing file has the path set to:

path: '/{user}/settoken'


To do what you're describing I would expect to see:

path: '/u/{user}/settoken'


Second, are you opposed to using a second parameter in the URL instead of the Get Method variable? If not opposed you can add a second parameter that will handle the 'token' variable.

In your routing file you can set up the pareters like this:

path: '/u/{user}/settoken/{token}'


The url would then be (cleaner I think):

/u/john-smith-36/settoken/123456


Lastly, to access the parmeters in your controller you shouldn't need to access the Globals. The parameters get passed to your function directly. Use this as the function declaration to pick up the variables:

public function SavePushToken($user, $token)