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
<?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->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.
$user->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 ?