Created
September 29, 2016 14:03
-
-
Save annikaC/722f0a38e734c087d6f0779a1aef6f3b to your computer and use it in GitHub Desktop.
Drupal 8 redirect user to own user edit page (for use in menu links)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\yourmodule\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
/** | |
* A controller that redirects to the current user's edit account page. | |
*/ | |
class AccountEditRedirectController extends ControllerBase { | |
/** | |
* Redirect to | |
*/ | |
public function redirect_to_account_edit() { | |
global $base_url; | |
$path = \Drupal\Core\Url::fromRoute('entity.user.edit_form', ['user' => \Drupal::currentUser()->id()])->toString(); | |
$response = new RedirectResponse($base_url . $path); | |
$response->send(); | |
} | |
} |
This works! ⬆️
I had totally forgotten about this gist! Thanks for commenting with your updates :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're redirecting to routes, you can utilize the
$this->redirect()
method. Then you don't work with URLs and don't need to trreat the base_url.