Laravel-adminlte: Different menus per route group

Created on 10 May 2016  路  8Comments  路  Source: jeroennoten/Laravel-AdminLTE

I was wondering, is there a more elegant way that we can set different menus based on route.
For example the admin group in the routes.php to have menu A and the user group to have menu B

Right now, what I've done is setting an empty menu in the config/adminlte.php and then in the routes.php, i'm creating the menu per group,

Route::group(['prefix' => 'admin'], function () {
    Route::get('dashboard', function () {

        Event::listen('JeroenNoten\LaravelAdminLte\Events\BuildingMenu', function ($event) {
            $event->menu->add('MAIN NAVIGATION');
            $event->menu->add([
                'text' => 'Blog',
                'url'  => 'admin/blog',
            ]);
        });

        return view('welcome');
    });
});

Sorry for all these questions, but I think that you have been through these issues before me, and it would be a pitty to recreate the wheel

Most helpful comment

Based on the answers provided above this is how i implemented mine,

  1. Cleared the adminlte.php menu because both groups will see different menus

001_admin_lte

2.created to files in the config directory to hold individual group route arrays

002_config_files

  1. The administration routes in the administration.php
    003_admin

and the other one
004_self_service

  1. I then created a middleware that accepts a menu type paramenter and used that to determine the route arrays to render

middlware

  1. Added the middleware to my route group routes

005_web_routes_with_middleware

  1. this renders different menu for the different route groups

Administration
administration_menu

Self Service

self_service_menu

You can aslo add permissions to the menu items,Hope this helps somebody, Any suggestions are welcomed

All 8 comments

You could solve this for example with a route group middleware. Run artisan make:middleware AdminMenu and artisan make:middleware UserMenu. You can find the created middleware in the app/Http/Middleware directory. Add the menu configuration code above return $next($request); in the middleware. In app/Http/Kernel.php, add 'menu.admin' => AdminMenu::class and 'menu.user' => UserMenu::class to the $routeMiddleware array (don't forget to import the classes at the top). Then in your routes.php:

Route::group(['prefix' => 'admin', 'middleware' => 'menu.admin'], function () {
    Route::get('dashboard', function () {
        return view('welcome');
    });
});

Route::group(['prefix' => 'user', 'middleware' => 'menu.user'], function () {
    Route::get('dashboard', function () {
        return view('welcome');
    });
});

Alternatively, you could use just one middleware with parameters if that is more convenient (for example if you have menu items that both groups need).

may I see your config/adminlte.php for the menu ??

how can I use route instead of url in menu configuration
example settings.index

Based on the answers provided above this is how i implemented mine,

  1. Cleared the adminlte.php menu because both groups will see different menus

001_admin_lte

2.created to files in the config directory to hold individual group route arrays

002_config_files

  1. The administration routes in the administration.php
    003_admin

and the other one
004_self_service

  1. I then created a middleware that accepts a menu type paramenter and used that to determine the route arrays to render

middlware

  1. Added the middleware to my route group routes

005_web_routes_with_middleware

  1. this renders different menu for the different route groups

Administration
administration_menu

Self Service

self_service_menu

You can aslo add permissions to the menu items,Hope this helps somebody, Any suggestions are welcomed

Based on the answers provided above this is how i implemented mine,

  1. Cleared the adminlte.php menu because both groups will see different menus

001_admin_lte

2.created to files in the config directory to hold individual group route arrays

002_config_files

  1. The administration routes in the administration.php
    003_admin

and the other one
004_self_service

  1. I then created a middleware that accepts a menu type paramenter and used that to determine the route arrays to render

middlware

  1. Added the middleware to my route group routes

005_web_routes_with_middleware

  1. this renders different menu for the different route groups

Administration
administration_menu

Self Service

self_service_menu

You can aslo add permissions to the menu items,Hope this helps somebody, Any suggestions are welcomed

Thank you very much. You saved my day.

Thank you very much. You saved my day.

you are most welcome, glad it helped

Excellent solution my friend! but... I keep getting : Class 'App\Http\MiddlewareEvent' not found

Any clue?

Excellent solution my friend! but... I keep getting : Class 'App\Http\MiddlewareEvent' not found

Any clue?

sorry to here that, These are the classes i included at the top of the file, I am guessing you need the last one
(use Illuminate\Support\FacadesEvent;). Try importing it.

use JeroenNoten\LaravelAdminLteEvents\BuildingMenu;
use Closure;
use Illuminate\Support\FacadesEvent;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iHeartBard picture iHeartBard  路  4Comments

niltonssjr picture niltonssjr  路  4Comments

Shidersz picture Shidersz  路  5Comments

ahishamali10 picture ahishamali10  路  5Comments

claytongf picture claytongf  路  5Comments