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

AntV's avatar
Level 6

routeIs for named route with parameter

Before I go on, I know (and I have) "fixed" this by using request()->is and using the path instead of the name, but I still feel the need to get to the bottom of this.

To keep it simple: This is my web.php:

Volt::route('/group/{group:slug}', 'shop/group-show')->name('groups.show');

This is how I route to it:

href="{{ route('groups.show', $group->slug) }}"

This returns true no matter the active group:

request()->routeIs('group.show', $group)

So do all of the following:

request()->routeIs('group.show', $group->slug)
request()->routeIs('group.show', ['group'=>$group])
request()->routeIs('group.show',['group'=> $group->slug])
request()->routeIs('group.show', ['group:slug'=>$group->slug])

I even went into the framework's GitHub and I see that routeIs should indeed work like I thought it should, as it accepts ...$patterns, that then each gets evaluated. What am I missing?

Edit: I am a moron, it goes through each statement and returns true if any of those statements are correct. I do not see why it is designed like that, but I can see some use case.

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're encountering is due to a misunderstanding of how routeIs works in Laravel. The routeIs method is used to check if the current route name matches a given pattern. It does not take route parameters into account. Instead, it only checks the route name itself.

In your case, you are trying to match the route name groups.show with parameters, but routeIs is not designed to handle parameters. It simply checks if the current route's name matches the specified pattern.

If you want to check if the current route is groups.show and also verify the parameter, you will need to do this in two steps:

  1. Check if the route name matches.
  2. Check if the parameter matches.

Here's how you can achieve this:

if (request()->routeIs('groups.show') && request()->route('group')->slug === $group->slug) {
    // The current route is 'groups.show' and the group slug matches
}

In this code:

  • request()->routeIs('groups.show') checks if the current route name is groups.show.
  • request()->route('group')->slug retrieves the group parameter from the route and compares its slug with $group->slug.

This approach ensures that both the route name and the parameter are correctly validated.

1 like
AntV's avatar
Level 6

@LaryAI that would make sense, but mayyyybe this should be in the documentation?

Please or to participate in this conversation.