Closed shankhadevpadam closed 3 years ago
Hi @shankhadevpadam , thanks for this feedback. Please try the next fix to this issue:
Edit the file vendor/jeroennoten/laravel-adminlte/src/Menu/ActiveChecker.php
on your local Laravel project and replace the method checkPattern()
by the next new version:
/**
* Checks if an url pattern matches the requested url.
*
* @param string $pattern
* @return bool
*/
protected function checkPattern($pattern)
{
// First, check if the pattern is a regular expression.
if (Str::startsWith($pattern, 'regex:')) {
$regex = Str::substr($pattern, 6);
return (bool) preg_match($regex, $this->request->path());
}
// If pattern is not a regex, check if the requested url matches the
// absolute path to the given pattern. When the pattern uses query
// parameters, compare with the full request url.
$pattern = preg_replace('@^https?://@', '*', $this->url->to($pattern));
$request = $this->request->url();
if (Str::contains($pattern, '?')) {
$request = $this->request->fullUrl();
}
return Str::is($pattern, $request);
}
And please, tell me if with this new version all works as you expected. Your commentaries about this will help to make a fix for this issue.
Perfectly work. another problem, if I used route instead of URL how to set active for the route.
for eg
[
'text' => 'Posts',
'url' => 'javascript:;',
'icon' => 'nav-icon far fa-edit',
'can' => 'view_posts',
'submenu' => [
[
'text' => 'All Posts',
'route' => 'admin.posts.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['admin/posts/*/edit'],
'can' => 'view_posts',
],
[
'text' => 'Add New',
'route' => 'admin.posts.create',
'icon' => 'nav-icon far fa-circle',
'can' => 'add_posts',
],
[
'text' => 'Categories',
'route' => ['admin.terms.index', ['type' => 'category']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_posts',
],
[
'text' => 'Tags',
'route' => ['admin.terms.index', ['type' => 'tag']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_posts',
],
],
],
and prefix "admin" should be dynamic. In my case i want to active "All Posts" when i edit the post.
Hi @shankhadevpadam thanks for your feedback, I will make a fix soon for the first mentioned problem.
I'm not very familiarized with the usage of the route
field, but for what I have seen, the name of the route
used on that field will be used to generated the href
attribute (an url) of the item's link. The ActiveChecker module is based on comparisons with the current requested URL, so you will need to know the URL's related to the routes. Knowing that, you can use regular expressions on the active
field to cover very particular cases (and I believe this is what you are searching for). You can see an example on Active Menu Items Section. But, you can do things like this one:
[
'text' => 'All Posts',
'route' => 'admin.posts.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/posts/.*/edit$@'],
'can' => 'view_posts',
],
That one should match urls like next ones (If the regular expression is not wrong):
foo/posts/1234/edit
bar/posts/09/edit
admin/posts/blabla/edit
All work perfectly now.
This is works in local development perfectly but not work in a live server.
@shankhadevpadam Hi, can you provide more info, like the pattern with query parameters that should be active and the full url you see in the browser when it should be active. Also, have you tried the version applied on the fix?
https://imgur.com/a/7jmEnB2 (Local development) https://imgur.com/VhNUhcG (My live testing server)
'menu' => [
[
'text' => 'search',
'search' => false,
'topnav' => false,
],
[
'text' => 'Home',
'route' => 'admin.home',
'icon' => 'nav-icon fas fa-house-user',
'label_color' => 'success',
],
[
'text' => 'Posts',
'url' => 'javascript:;',
'icon' => 'nav-icon far fa-edit',
'can' => 'view_posts',
'submenu' => [
[
'text' => 'All Posts',
'route' => 'admin.posts.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/posts/.*/edit$@'],
'can' => 'view_posts',
],
[
'text' => 'Add New',
'route' => 'admin.posts.create',
'icon' => 'nav-icon far fa-circle',
'can' => 'add_posts',
],
[
'text' => 'Categories',
'route' => ['admin.terms.index', ['type' => 'category']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_terms',
],
[
'text' => 'Tags',
'route' => ['admin.terms.index', ['type' => 'tag']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_terms',
],
],
],
[
'text' => 'Pages',
'url' => 'javascript:;',
'icon' => 'nav-icon far fa-sticky-note',
'label_color' => 'success',
'can' => 'view_pages',
'submenu' => [
[
'text' => 'All Pages',
'route' => 'admin.pages.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/pages/.*/edit$@'],
'can' => 'view_pages',
],
[
'text' => 'Add New',
'route' => 'admin.pages.create',
'icon' => 'nav-icon far fa-circle',
'can' => 'add_pages',
],
]
],
[
'text' => 'Appearance',
'url' => 'javascript:;',
'icon' => 'nav-icon fas fa-paint-brush',
'label_color' => 'success',
'can' => 'view_menus',
'submenu' => [
[
'text' => 'Menus',
'route' => 'admin.menus.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/menus/.*/edit$@', 'regex:@^.*/menus/create$@'],
'can' => 'view_menus',
],
],
],
[
'text' => 'Settings',
'url' => 'javascript:;',
'icon' => 'nav-icon fas fa-tools',
'label_color' => 'success',
'can' => 'view_settings',
'submenu' => [
[
'text' => 'General',
'route' => ['admin.settings.index', ['option' => 'general']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_settings',
],
]
],
['header' => 'account_settings'],
[
'text' => 'Users',
'route' => 'admin.users.index',
'icon' => 'nav-icon far fa-user',
'can' => 'view_users',
],
[
'text' => 'Roles',
'route' => 'admin.roles.index',
'icon' => 'nav-icon fas fa-user-tag',
'can' => 'view_roles',
],
[
'text' => 'profile',
'route' => 'admin.users.profile',
'icon' => 'nav-icon fas fa-address-card',
],
],
Please, can you provide the route definition for ['admin.terms.index', ['type' => 'category']]
?
Also, can you temporarily change the checkPattern()
method of the file vendor/jeroennoten/laravel-adminlte/src/Menu/ActiveChecker.php
by next one:
/**
* Checks if an url pattern matches the requested url.
*
* @param string $pattern
* @return bool
*/
protected function checkPattern($pattern)
{
Log::error("");
Log::error("#####> pattern is: {$pattern} ---------------------------");
// First, check if the pattern is a regular expression.
if (Str::startsWith($pattern, 'regex:')) {
$regex = Str::substr($pattern, 6);
return (bool) preg_match($regex, $this->request->path());
}
// If pattern is not a regex, check if the requested url matches the
// absolute path to the given pattern. When the pattern uses query
// parameters, compare with the full url request.
$pattern = preg_replace('@^https?://@', '*', $this->url->to($pattern));
$request = $this->request->url();
if (isset(parse_url($pattern)['query'])) {
$request = $this->request->fullUrl();
}
Log::error("#####> normalized pattern is: {$pattern}");
Log::error("#####> request is: {$request}");
return Str::is($pattern, $request);
}
and add use Illuminate\Support\Facades\Log;
at the begin of the same file. Then after clicking on the categories menu item, open the file storage/logs/laravel.log
and locate the debug related to the categories
pattern to show me that information. The debug should be something like this:
[2020-09-20 14:41:23] local.ERROR: #####> pattern is: <categories_pattern> ---------------------------
[2020-09-20 14:41:23] local.ERROR: #####> normalized pattern is: <normalized_categories_patetrn>
[2020-09-20 14:41:23] local.ERROR: #####> request is: https://easycms...
I get this one.
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: # ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: #
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/home ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/home
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: regex:@^.*/posts/.*/edit$@ ---------------------------
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/posts ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/posts
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/posts/create ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/posts/create
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/terms?type=category ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/terms?type=category
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms?type=category
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/terms?type=tag ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/terms?type=tag
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms?type=category
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/posts ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/posts
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/posts/create ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/posts/create
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: regex:@^.*/pages/.*/edit$@ ---------------------------
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/pages ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/pages
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/pages/create ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/pages/create
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/pages ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/pages
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/pages/create ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/pages/create
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: regex:@^.*/menus/.*/edit$@ ---------------------------
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/menus ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/menus
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/menus ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/menus
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/settings?option=general ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/settings?option=general
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms?type=category
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/settings?option=general ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/settings?option=general
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms?type=category
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: javascript:; ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/javascript:;
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/users ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/users
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/roles ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/roles
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
[2020-09-21 04:04:53] local.ERROR:
[2020-09-21 04:04:53] local.ERROR: #####> pattern is: https://example.com/admin/profile ---------------------------
[2020-09-21 04:04:53] local.ERROR: #####> normalized pattern is: *example.com/admin/profile
[2020-09-21 04:04:53] local.ERROR: #####> request is: https://example.com/admin/terms
That is really strange, for what I can see on the next lines it should work:
#####> pattern is: https://easycms.padamshankhadev.com/admin/terms?type=category ---------------------------
#####> normalized pattern is: *easycms.padamshankhadev.com/admin/terms?type=category
#####> request is: https://easycms.padamshankhadev.com/admin/terms?type=category
Just in case, try adding trim()
method on the return
sentence and an extra log:
Log::error('#####> check result: ' . Str::is($pattern, $request));
return Str::is(trim($pattern), trim($request));
Also, what is the order of filters on the filters configuration option of the adminlte.php
file in the production environment?
'menu' => [
[
'text' => 'search',
'search' => false,
'topnav' => false,
],
[
'text' => 'Home',
'route' => 'admin.home',
'icon' => 'nav-icon fas fa-house-user',
'label_color' => 'success',
],
[
'text' => 'Posts',
'url' => 'javascript:;',
'icon' => 'nav-icon far fa-edit',
'can' => 'view_posts',
'submenu' => [
[
'text' => 'All Posts',
'route' => 'admin.posts.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/posts/.*/edit$@'],
'can' => 'view_posts',
],
[
'text' => 'Add New',
'route' => 'admin.posts.create',
'icon' => 'nav-icon far fa-circle',
'can' => 'add_posts',
],
[
'text' => 'Categories',
'route' => ['admin.terms.index', ['type' => 'category']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_terms',
],
[
'text' => 'Tags',
'route' => ['admin.terms.index', ['type' => 'tag']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_terms',
],
],
],
[
'text' => 'Pages',
'url' => 'javascript:;',
'icon' => 'nav-icon far fa-sticky-note',
'label_color' => 'success',
'can' => 'view_pages',
'submenu' => [
[
'text' => 'All Pages',
'route' => 'admin.pages.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/pages/.*/edit$@'],
'can' => 'view_pages',
],
[
'text' => 'Add New',
'route' => 'admin.pages.create',
'icon' => 'nav-icon far fa-circle',
'can' => 'add_pages',
],
]
],
[
'text' => 'Appearance',
'url' => 'javascript:;',
'icon' => 'nav-icon fas fa-paint-brush',
'label_color' => 'success',
'can' => 'view_menus',
'submenu' => [
[
'text' => 'Menus',
'route' => 'admin.menus.index',
'icon' => 'nav-icon far fa-circle',
'active' => ['regex:@^.*/menus/.*/edit$@', 'regex:@^.*/menus/create$@'],
'can' => 'view_menus',
],
],
],
[
'text' => 'Settings',
'url' => 'javascript:;',
'icon' => 'nav-icon fas fa-tools',
'label_color' => 'success',
'can' => 'view_settings',
'submenu' => [
[
'text' => 'General',
'route' => ['admin.settings.index', ['option' => 'general']],
'icon' => 'nav-icon far fa-circle',
'can' => 'view_settings',
],
]
],
['header' => 'account_settings'],
[
'text' => 'Users',
'route' => 'admin.users.index',
'icon' => 'nav-icon far fa-user',
'can' => 'view_users',
],
[
'text' => 'Roles',
'route' => 'admin.roles.index',
'icon' => 'nav-icon fas fa-user-tag',
'can' => 'view_roles',
],
[
'text' => 'profile',
'route' => 'admin.users.profile',
'icon' => 'nav-icon fas fa-address-card',
],
],
/*
|--------------------------------------------------------------------------
| Menu Filters
|--------------------------------------------------------------------------
|
| Here we can modify the menu filters of the admin panel.
|
| For more detailed instructions you can look here:
| https://github.com/jeroennoten/Laravel-AdminLTE/#612-menu-filters
|
*/
'filters' => [
JeroenNoten\LaravelAdminLte\Menu\Filters\HrefFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\SearchFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\ActiveFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\ClassesFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\DataFilter::class,
],
The filters are ok. Test with the mentioned changes, you shoud see something like next for the categories comparison log:
#####> pattern is: https://easycms.padamshankhadev.com/admin/terms?type=category ---------------------------
#####> normalized pattern is: *easycms.padamshankhadev.com/admin/terms?type=category
#####> request is: https://easycms.padamshankhadev.com/admin/terms?type=category
#####> check result: 1 (or true)
If you see that, then the problem arises in other place. If the result is a 0
or false
we should check why this is happening.
Now this work fine.
@shankhadevpadam Can you explain what fixed it? or what was the problem? So I can know if a fix is needed...
I don't know what the problem. But when I paste the code you send last one, that fix the problem.
@shankhadevpadam Please, can you confirm the next line fixed the problem?
return Str::is(trim($pattern), trim($request));
By executing next steps:
return Str::is($pattern, $request);
and confirm the categories menu is not active.return Str::is(trim($pattern), trim($request));
and confirm the categories menu is now active.That will be very helpful for us. Also, take in mind you should delete all the added debug
lines.
return Str::is(trim($pattern), trim($request));
This one fix the problem. And I already deleted the log.
I have installed the latest version of the package. But I have a problem in the submenu with a query string.
if i set active with query string in categories and tags, both menu are not active. If i set active without query string like ['admin/terms] then both menu will be selected if i click categories or tags. in categories and tags.