laravelista / Ekko

Framework agnostic PHP package for marking navigation items active.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support route wildcards?

judgej opened this issue · comments

Resource model routes have a bunch of predefined route names that follow a pattern. For example, to administer a user, you may define the user resource route like this:

Route::resource('users', 'UserController');

That generates the routes users.index, users.show, users.create etc.

Instead of having to list all these routes in an array, it would be great to be able to use a wildcard. a simple globbling approach would be work well, without the overhead of REs:

class="{{ Ekko::isActiveRoute('users.*') }}"

Maybe this is already supported in some way?

This is the way I handle this:

  1. List all routes in array with areActiveRoutes(['users.index', 'users.show']) or
  2. Use isActiveMatch('users') which looks for string users in the current URL.

Thanks. I want to avoid (1) as it is cumbersome. Also I wanted to avoid (2) as it is not specific enough on the parts of the URL it matches. I'll see what I can do in a fork, as it looks like the route name matching is just one line in the code that can easily be extended. Maybe a PR if you are happy with the approach.

If you could do a PR that would be great. I haven't had the time lately to add that feature myself so any help is welcome.

I've made a tweak to allow * as a wildcard character. I'm using for the menu option to administer users in my admin section using a resource controller routing like this: admin.users.*

That way the "administer users" menu item is active no mater whether I'm adding, editing, listing etc. users. Having full wildcards support may be another option, but this kind of forces you to keep the views simpler. If I find it's not sufficient as my app develops, I may well find it necessary to add full RE support, but I'll see how it goes.

Feel free to merge if you like it, or wait until I do a PR after I've run with it for a while :-)

I created a function for this:

public function isActiveRouteMatch($string, $output = "active")
{
        if(str_contains(Route::currentRouteName(), $string)) return $output;

        return null;
    }

<li class="{{ Ekko::isActiveRouteMatch('admin.assinantes') }}"><a href="{{ route('admin.assinantes.index') }}"><i class="fa fa-users"></i> <span>Assinantes</span></a></li>

@diorgesl I lile this one. Could you send a PR for this?

I've updated the docs and extended the wildcard match to multiple route names too (areActiveRoutes()). I'm using it at the moment for nested resource routes (as shown the docs).

The wildcard is an asterisk (*) only and matches non-period/full-stop (.) characters. It is not a full RE match, which could possibly be added as a separate rule. The intend is that no current route matches will be affected, but the wildcard can be easily used where needed without introducing new API methods.

I didn't go for the "contains" match used by the other matching methods, as they are liable to give too many false-positives. For example, I have a "user" resource controller, and also a "foobar.user" nested resource controller. I want to match "user.index", "user.show" etc. for one menu item, without matching "foobar.user.index", "foobar.user.show" etc. which are matched to a separate "foobar" menu item. My change allows me to use "user." and "foobar.user." as separate route name matches that don't overlap.

Thank you. Will be released in version 1.2.0

Thank you :-)

Using 1.2.0 now, and all works wonderfully (after switching my PHP from 5.5 to 5.6).