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

troccoli's avatar

How can I check if a URL is a named route?

When reaching a page in my app I need to find out where the user is coming from. I can get the URL from the referer header in the request.

I could then check it against hardcoded URL, but 1) I'd rather use named route so I don't have "magic" strings, and 2) some of the URL have parameters (mostly a model slug) so I can't really do a comparison. I thought about using the startsWith() string method, but that will still leave me with "magic" strings.

$referer = str(request()->header('referer'));
if ($referer->is(route('datasets.all'))) {
    // Do something
} elseif ($referer->startsWith(config('app.url').'/reports')) {
    // Do something
} else {
    // Do something
}

I know the Request facade as a routeIs() method that does what I'm trying to do, but that works on the current URL. Ideally there would be a method in the Url facade, so I could do something like

$referer = url(request()->header('referer'));
if ($referer->is(route('datasets.all'))) {
    // Do something
} elseif ($referer->is(route('reports.view'))) {
    // Do something
} else {
    // Do something
}

but such method doesn't exist.

Does anybody has any suggestions on how I can check if a URL, which is not the current one, is a named route?

0 likes
11 replies
LaryAI's avatar
Level 58

Great question! Laravel does not provide a built-in way to resolve a full URL (like a referer) back to a named route directly. However, you can achieve this by using the router to match the referer URL and then check its route name.

Here’s how you can do it:

  1. Parse the referer URL to get the path.
  2. Use the router to match the path to a route.
  3. Check the route's name.

Here’s a practical example:

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

$referer = request()->header('referer');

if ($referer) {
    // Parse the path from the referer URL
    $refererPath = parse_url($referer, PHP_URL_PATH);

    // Create a new Request instance for the referer path
    $fakeRequest = Request::create($refererPath);

    // Match the request to a route
    $route = app('router')->getRoutes()->match($fakeRequest);

    // Now you can check the route name
    if ($route->getName() === 'datasets.all') {
        // Do something
    } elseif ($route->getName() === 'reports.view') {
        // Do something else
    } else {
        // Fallback
    }
}

Notes:

  • This approach works even if your routes have parameters (like slugs or IDs).
  • If the referer is from an external site or doesn't match any route, $route->getName() will be null.
  • Always validate and sanitize the referer, as it can be spoofed.

Summary:
You can match a referer URL to a named route by parsing the path, creating a fake request, and using Laravel's router to resolve the route and check its name. This avoids hardcoding URLs and works with dynamic parameters.

vincent15000's avatar

I would simply generate the named route with the route() helper and compare it with the URL.

if (route('name.route') === $url) {
	...
} else {
	...
}
JussiMannisto's avatar

@vincent15000 The point is, you have to do more than a simple comparison.

And what about a route like /users/{user}/edit? Your method would throw an exception due to a missing route parameter in the route() call.

1 like
JussiMannisto's avatar

@vincent15000 And it doesn't work if you don't. The user resource was just an example. If you already knew the details of the referer URL in advance, this whole question would be kind of pointless.

1 like
Glukinho's avatar

Maybe this way?

$referer_fake_request = Request::create($referer_url);

$referer_fake_request->routeIs('datasets.all'); // true or false
1 like
JussiMannisto's avatar

Did you consider using url()->previous() instead of relying on the Referer header? Remember that Referer, like all headers, can be spoofed by the client, so be careful about how you use it.

1 like
troccoli's avatar

Sorry, I thought I had replied with my solution. It would have save you some time, sorry.

I used @laryai reply as a starting point and I ended with something like

$referer = Request::header('referer') ?? URL::current();
$fakeRequest = Request::create($referer);
$route = Route::getRoutes()->match($fakeRequest);
$routeName = $route->getName();
if ($routeName === 'datasets.all') {
    // Do something
} elseif ($routeName === 'reports.view') {
    // Do something
} else {
    // Do something
}

And it works for routes with parameters (I didn't try with query parameters, but since I want to check the route I don't care about query parameters).

1 like

Please or to participate in this conversation.