Retrieve the anchor from the called URL

I need to redirect a user to a page but need to extract the anchor tag from the called url (to put it on the redirect url)
I have try Router::getRequest()->getUri() but I don’t see the anchor tag in it.

What is the best way to extract the anchor tag from the called URL in cakePHP 4


What you are probably looking for is the HTTP referer header which points to the original URL the user was on before he clicked on the anchor tag.

$referer = $this->getRequest()->referer();

yes and no, in fact I have to add a GET parameter to that url, so I have to :
1/ extract the anchor
2/ add the parameter
3/ re-add the anchor

adding the parameter after the anchor don’t work.

example : /mypage?param=1#anchor should become /mypage?param=1&param=2#anchor

also if the user come from google, It won’t work (but I am agree check that the host is mine is possible :slight_smile: )


I would recommend you look at authentication/AuthenticationService.php at 2.x · cakephp/authentication · GitHub which does basically the same.

You need to encode your desired URL as a GET parameter (via urlencode()) so whatever logic you have in your redirected page can extract that urlencoded data and urldecode() it.

You can’t just put a whole URL without any encoding inside another URL.