-1

I would like to add a dynamic path '/myprofile' as an alias for /user/{uid}/profile . (Always using the current users id.) If possible I don't want to use redirect.

I tried to do the following in my controller with a route /myprofile:

public function myProfile() {
  $sub_request = Request::create('/user/' . $this->currentUser->id() . '/profile', 'GET');
  $subResponse = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);

  return $subResponse;

}

Somehow this seems to work, as the page is displayed correctly but somehow the form is not working anymore. When I update the values and save nothing happens.

I also thought about adding a new route in a route subscriber, but I don't know how. https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones does not help, as the last part about adding routes does not contain any example.

0

Your sub request doesn't work for a form. When you submit the form the POST request can't reach the form inside of the sub request. So you probably would need to work on building a better request which contains the POST data. But I don't think you need a sub request, you can return the user profile directly from the controller:

public function myProfile() {
  $user = User::load($this->currentUser->id());
  return $this->entityFormBuilder()->getForm($user);
}
  • works. But I don't understand why the surequest doesn't work. Would appreciate if you can explain the problem. – Miguel Jun 26 '18 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.