lavary/laravel-menu

Active Item Question

Closed this issue · 6 comments

Hello,
I have a question about using the "Active Item", I would like to have a help with that.

I have 2 URL patterns, where the ID is dynamic at each URL. example:

  1. /painel/produtos/ID123/editar
  2. /painel/produtos/categorias/ID123/editar

I am trying to use the following rule for the active() method:
->active("painel/produtos/criar")->active("painel/produtos/*/editar")

This rule is ok in the example url /painel/produtos/ID123/editar
image

But at URL /painel/produtos/categorias/ID123/editar it ends up activating the menu of the previous link, which is not true.
image

config/settings.php

return [
    'default' => [
        'auto_activate'    => TRUE,
        'activate_parents' => TRUE,
        'active_class'     => 'active',
        'restful'          => FALSE,
        'cascade_data'     => TRUE,
        'rest_base'        => '',      // string|array
        'active_element'   => 'item',  // item|link
    ],
    'menu_site' => [
        'active_class'     => 'active',
    ],
    'metronic_aside' => [
        'active_class'     => 'kt-menu__item--active kt-menu__item--open',
    ],
];

The question is, how can I solve this? what's the best way?

thank you in advance for your help.

Taking advantage of the question, it is correct to use the following form:
->active("painel/produtos/criar", "painel/produtos/*/editar")
or separate the methods as I mentioned earlier?

You can see the ->active(...) function here:
https://github.com/lavary/laravel-menu/blob/master/src/Lavary/Menu/Item.php#L462

You can see that it does not take a second parameter.

You can also see why it is matching categories/ID123 when you don't want it to because it is simply using a replace and regex on .* which includes the / directory separator symbol.

You could write your own function that checks if it matches a different regex, then call ->activate() directly. It's simply calling request()->path() which you can pull in easily enough.

Let me know if that makes sense.

You can see the ->active(...) function here:
https://github.com/lavary/laravel-menu/blob/master/src/Lavary/Menu/Item.php#L462

You can see that it does not take a second parameter.

You can also see why it is matching categories/ID123 when you don't want it to because it is simply using a replace and regex on .* which includes the / directory separator symbol.

You could write your own function that checks if it matches a different regex, then call ->activate() directly. It's simply calling request()->path() which you can pull in easily enough.

Let me know if that makes sense.

Hello, thanks for the feedback. I would not like to write a new method but use what is already available if possible.

So I can't use the symbol *, in its place I should use a regular expression where I only take letters(upper and lowercase) and numbers. Something like that, is it? I didn't quite understand that part..

Would you have any example of how I can do with these 2 urls?
/painel/produtos/ID123/editar
/painel/produtos/categorias/ID123/editar

I am not set up to do this or test this locally. My previous comment should give you what you need.

Take a look at the function, it uses preg_replace looking for /*, but you can provide your own pattern. If you avoid /* then it won't rewrite your regex pattern, and will use whatever you provide.

Edit: Fixed slashes.

Ok, I get it. I did the following way:

image

I used the following method in the method: ->active("painel/produtos/((?i)[0-9a-z]*)/editar")

Now i want the standard: painel/produtos/criar,

in this case I do it this way: ->active("painel/produtos/criar")->active("painel/produtos/((?i)[0-9a-z]*)/editar")

Would I have a problem or can I use this pattern?

It looks like you're on the right track.

Give it a try and see if it works.

Yes, it works. I just wanted to make sure not to make a "mess" =)

Thank you for your help! I will close this issue.