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

Ligonsker's avatar

Is it possible to get the URL from which a link was clicked?

Hello,

If I am on a page my-url/origin, and this page redirects to other routes within web.php, for example via <a> tag: <a href="other-route">Other Route</a>, then this route redirects to a controller method:

Route::get('other-route', [SomeController::class, 'otherRouteAction']);

Is it possible to get the original URL this route was referred from in the controller?

public function otherRouteAction(Request $request) 
{
   // get the origin URL
}

Ty

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, it is possible to get the original URL from which a link was clicked. You can achieve this by passing the original URL as a query parameter in the link and then retrieving it in the controller using the Request object.

Here's an example:

In your view file, add the original URL as a query parameter in the link:

<a href="{{ route('other-route', ['origin' => url()->current()]) }}">Other Route</a>

In your web.php file, define the route with the origin parameter:

Route::get('/other-route', [SomeController::class, 'otherRouteAction'])->name('other-route');

In your controller method, retrieve the origin parameter from the Request object:

public function otherRouteAction(Request $request) 
{
   $origin = $request->query('origin');
   // use the $origin variable as needed
}

Note: This solution assumes that the original URL is the current URL. If you need to get the URL of the previous page, you can use the referer header instead of passing it as a query parameter.

1 like

Please or to participate in this conversation.