Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitsolanki24_'s avatar

Get fragment from url in controller

public/search?q=joy#user

How can I get #user fragment in laravel controller, any idea?

0 likes
5 replies
JussiMannisto's avatar

You can't. URI fragments are not sent to the server.

Your URLs shouldn't start with public/, unless that's part of the route definition. It's an indicator of server misconfiguration and can be a huge security issue.

1 like
martinbean's avatar

@amitsolanki24_ As @jussimannisto says, you can’t as the fragment is only use on the client-side.

Why are you trying to access the fragment in PHP in the first place? If you’re trying to use it for query then you should be properly using query string values for that:

/search?q=joy&type=users

You also shouldn’t have “public” at the beginning of URIs. That suggests you’ve not set your project up correctly, and potentially exposing files that should’t be accessible, like your .env file for starters 😬

1 like
amitsolanki24_'s avatar
amitsolanki24_
OP
Best Answer
Level 8

@amitsolanki24_ I found a solution, how can we get a fragment from url ?

Here it the solution

use Illuminate\Support\Uri;
$fragment = Uri::of('https://www.game.com/#comments')->fragment();
echo $fragment; // output: comments

1 like
JussiMannisto's avatar

@amitsolanki24_ You just wanted to parse the fragment from a URI string? What did the in laravel controller part mean? That made it sound like you wanted the fragment of the request url on the server side (where it doesn't exist).

Please or to participate in this conversation.