Epyc's avatar
Level 7

Find named routes with wildcard

Even though I'm not convinced that named routes are useful, I decided to give them a try anyway.

All works good, but for my navigation I would like to highlight the active item. Now, highlighting home.index when it's active is easy.

But now take a full resource route, for example "ingredients". I want the navigation item "ingredients" to be active when the user is on ingredients.index, but also ingredients.create etc...

With URL's this is easy, because I could pass wildcards into \Request::is('ingredients*')

However, with routes I really don't find a convenient way to do this. I was thinking of grouping the routes, and then checking if the current route is in that specific route group, but I can't get that to work either (how to get the group of an item? How to even give the group a name without having an impact on the name of the resource routes themselves?).

How do you guys do this?

0 likes
4 replies
pmall's avatar

Most people stay with the Request::is('...') method.

I like to have a view composer for this :

view()->composer('*', function(View $view) use ($request) {

    $view->with('active', $request->route()->getName());

});

Put this above in the boot method of a service provider. Then all your views will have an $active variable with the name of the current route.

I'm not convinced that named routes are useful

Huh ? Dont you think updating all your urls in one place (the route file) is useful ?

Epyc's avatar
Level 7

Thanks for your reply.

Wouldn't working with Request::is() give a false sense of security? People think they only need to edit the routes file and everything will keep working, but actually they would also need to edit the navigation settings (and possibly other places where I'd like to use this functionality), because those still work based on URL.

Your view composer is a good idea, but then I only have the name of the active route. Then I still don't have a way to check if the active route is part of a "group" of routes.

And a bit off-topic (I guess I shouldn't have brought it up then lol): in 12 years I haven't ever had the need to change URL's. Even if I ever needed to do it, the overhead of working with named routes instead of simply typing URL's would be a lot more time lost than the 10 minutes it would take to do a few string replacements. Even with named routes, what happens if you change "users" to "members"? Your route name would stay "users", but the URL would be "members". Wouldn't that be incredibly confusing for other developers as well? Wouldn't it be better to then also update the route name? And then what is the point of using named routes. Just my opinion. I like to keep things simple if possible. But I'm willing to give it a try anyway, so that's where my question comes from. :-)

pmall's avatar

Your view composer is a good idea, but then I only have the name of the active route. Then I still don't have a way to check if the active route is part of a "group" of routes.

Array mapping. Also you can even make your own helper function to find a route group key by using wildcards :p

Also I like how the url are generated from route names and hate concatenating strings/ids to generate urls in my views. It made sense many times for me to use 'posts' internally to refer to some kind of entity and to display it something else in the url for presentation/seo/etc concerns. (Like video has many posts, a post containing the video is consulted on the website but I rather have videos/{slug} in the url than posts/{slug})

I think you'll agree with me that this is not a big design issue. Do what suits you and have fun.

usman's avatar
usman
Best Answer
Level 27

@Epyc Try:

Route::is($routePattern);
//or
app('router')->is($routePattern);

:)

1 like

Please or to participate in this conversation.