lavary/laravel-menu

Methods get and item don't work with utf8 titles

Opened this issue · 9 comments

dlnsk commented

\Menu::get('sbMenu')->get('Пользователи'); // means Users
\Menu::get('sbMenu')->item('Пользователи');
gets null

        Menu::make('sbMenu', function(){});
        Menu::get('sbMenu')->add('Пользователи');
        $x = Menu::get('sbMenu')->get('Пользователи');
        dd($x);

Seems to work for me. Did you add Пользователи to the menu first?

dlnsk commented

Yes. Of course.
I have copied your example and it is still buggy. If I change Пользователи to something in English it starts to work as expected.

PHP 7.3

What do you mean by still buggy? Are you inspecting the results of each get and add? I wouldn't be surprised if it's not utf8 friendly, but I don't know what needs to be changed. I tried the example I provided and it returned a menu item in $x

dlnsk commented
Menu::make('sbMenu', function(){});
$m = Menu::get('sbMenu')->add('Пользователи');
$x = Menu::get('sbMenu')->get('Пользователи');

If I use dd($m), I see Item object, but if dd($x) it returns null. If I change Пользователи to qwerty both variables contain Item.

Strange, that's exactly what I tried. I wonder if there are other differences in my php configuration. Have you seen this before, is there any common technique or shortfall of supporting utf8?

dlnsk commented
0 => Item {#570 ▼
        #builder: Builder {#671}
        #id: "id-5e58a7a4ba749253778274"
        +title: "Пользователи"
        +beforeHTML: null
        +afterHTML: null
        +nickname: "polzovateli"
        +divider: []
        #parent: null
        #link: Link {#613 ▶}
        #data: []
        #active: false
        +attributes: []
        +isActive: false
        -disableActivationByURL: false
      }

This is because you search in nickname.

    public function get($title)
    {
        return $this->whereNickname($title)->first();
    }

I suppose you could set the nickname manually. ->nickname('Пользователи') but, yes the search by nickname is pretty core to this package. If we change that, it could break many implementations.

Based on your findings, if you try ->get('polzovateli') it will probably retrieve the item (just as proof of concept.)

dlnsk commented

Based on your findings, if you try ->get('polzovateli') it will probably retrieve the item (just as proof of concept.)

Transliteration is a magic because nobody know how it converts cyrilic text. :)

I suppose you could set the nickname manually. ->nickname('Пользователи') but, yes the search by nickname is pretty core to this package. If we change that, it could break many implementations.

I think you should improve documentation because now it explicitly says Get Item by Title and not describe principle of operation of get and item.
For support backward compatibles I offer to add method getByTitle because to add nicknames manually to all items seems not convenient especially if using automatic generation.

Both good ideas, and good points.