laravel-menu icon indicating copy to clipboard operation
laravel-menu copied to clipboard

Menu item not active when URL defined for item has a query parameter

Open gtapps opened this issue 7 years ago • 4 comments

Hey guys,

The menu item doesn't get activated when the URL has a query parameter.

Example: $menu->add('Item 1', route('bananas', ['order_by' => 'free']));

On these scenarios, it is never set to active.

Cheers,

gtapps avatar May 13 '18 05:05 gtapps

Not sure if that's a bug or a feature enhancement.

Seems like support could be added, so just a totally aside thought, but perhaps you could use optional route parameters instead of query strings.

dustingraham avatar May 13 '18 19:05 dustingraham

Tried it also with the route paremeters and doesnt work either.

Thanks,

gtapps avatar May 13 '18 20:05 gtapps

https://github.com/lavary/laravel-menu#named-routes

What did you try? Can you provide some more code of your routes and menu that are not working?

dustingraham avatar May 13 '18 20:05 dustingraham

Seems as though it may not set the active class if the current URL is using optional parameters but the link in the menu is not? For example:

// Suppose we have these routes defined in our app/routes.php file

//...
Route::get('/{test?}',        ['as' => 'home.page',  function(){...}]);
//...

// Now we make the menu:
Menu::make('MyNavBar', function($menu){
  $menu->add('Home',     ['route'  => 'home.page']);
});

So if you visit http://localhost/ then it will get the class but http://localhost/abc will not.

My workaround is:

$class = ($request->routeIs('home.page')) ? 'active' : '';
$menu->add('Home', ['url' => route('home.page'), 'class' => $class]);

jeremy-smith-maco avatar Oct 29 '19 02:10 jeremy-smith-maco