How to redirect programatically using a route with parameters? I am trying to use the Passwordless module API to redirect the user on login.

I want to redirect the user to a specific node edit page (node/12/edit).

In hook_passwordless_login_redirect(), the module redirects the user with this code:

return $this-&gt;redirect($redirect);

So here is my implementation:

function MYMODULE_passwordless_login_redirect_alter(&amp;$redirect) {
  // You can use hasPermission() with currentUser, but need to load for hasRole()
  $account = \Drupal\user\Entity\user::load(\Drupal::currentUser()-&gt;id());
  $test = \Drupal::service('path.validator')-&gt;getUrlIfValid('node/12/edit');
  if (!($account-&gt;hasRole('admin'))) {
    $redirect= $test-&gt;getRouteName();
  }


The problem is that I get this error:


  Some mandatory parameters are missing ("node") to generate a URL for
  route "entity.node.edit_form".


The missing route parameter is the node id, which I can get with $test-&gt;getRouteParameters().  But how do I pass this to the redirect?  (How do I call the redirect and give it both the route name and the route parameters?)
As pointed out by @4k4, the problem is that the module did not ask for the route parameters, so I patched it.

Then you can call the redirect like this: 

  if (!($account-&gt;hasRole('admin'))) {
    $routename = $test-&gt;getRouteName();
    $routeparameters= $test-&gt;getRouteParameters();
  }