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:

  1. Fetch the account of the current logged in user.
  2. Update the text field field_user_push_token with the query parameter value: 123456
  3. Save the account of the current logged in user.
  4. 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 !


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



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->query->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()->id());

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

// Save the (current logged user) account.

// 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 ?

  • 1
    Isn’t this because a route can’t start with a parameter?
    – Kevin
    Apr 26, 2019 at 4:32

1 Answer 1


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):


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)
  • 1
    P.S. The $user variable being passed in via the parameter won't be a user object, it'll just be a string. I see you using the user object, also with a variable named $user, to call a couple methods. Don't confuse the two and name one of them something else. Apr 26, 2019 at 4:48
  • thank you for your point and I was doing exactly as your point but what really confused me is this documentation here which says: There is nothing more to provide in the route for the parameter conversion to happen because user is the name of an entity type and it will get automatically converted into a user object. In the controller, define an argument on the controller method with the same name to get this user object passed in Apr 27, 2019 at 0:58
  • the problem was because of using public function SavePushToken(AccountInterface $user, Request $request) which i took from here... The right function use is to declare the variable within the function in this way: public function SavePushToken($user, $token) Thank you and answer accepted... God bless Apr 27, 2019 at 1:01

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.