Skip to content

Events

This package dispatches a small set of Laravel events so your application can hook into the admin panel without publishing and editing any view. This page is the reference of all of them, together with the payload each one carries.

NOTE

An event is just a signal the package sends while it renders the panel. A listener is a piece of your own code that reacts to it. If you have never written one, read the Laravel Events documentation first, the examples below only show what to put inside the listener.

Summary

EventDispatched whenPayloadTypical use
BuildingMenuThe sidebar and topbar menu is being built$menuBuild the menu from a database or from the current user
ReadingDarkModePreferenceThe layout is about to resolve the color mode$darkModeRestore a per user color mode from your storage
DarkModeWasToggledThe server side dark mode toggle was hit$darkModePersist the new color mode in your storage
ScreenWasLockedThe session was flagged as locked$lockscreen, $userWrite an audit log entry
ScreenWasUnlockedThe password prompt was passed$lockscreen, $userWrite an audit log entry

Every event class lives in the JeroenNoten\LaravelAdminLte\Events namespace.

Where the Listener Goes

On a Laravel 12 or newer application there are two usual places for a listener, and the package works with both of them:

  • A listener class in app/Listeners/, which Laravel discovers automatically from the type hint of its handle() method. This is the recommended way.

    php
    <?php
    // app/Listeners/BuildAdminLteMenu.php
    
    namespace App\Listeners;
    
    use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
    
    class BuildAdminLteMenu
    {
        public function handle(BuildingMenu $event): void
        {
            // React to the event here...
        }
    }
  • A closure registered on the boot() method of any service provider, usually app/Providers/AppServiceProvider.php:

    php
    <?php
    // app/Providers/AppServiceProvider.php
    
    namespace App\Providers;
    
    use Illuminate\Support\Facades\Event;
    use Illuminate\Support\ServiceProvider;
    use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function boot(): void
        {
            Event::listen(BuildingMenu::class, function (BuildingMenu $event) {
                // React to the event here...
            });
        }
    }

IMPORTANT

The old app/Providers/EventServiceProvider.php file does not exist anymore on a fresh Laravel 12 application. Some examples of this documentation still show it, because it keeps working when your project has one, but the two shapes above are the current ones.

BuildingMenu

Dispatched every time the menu of the panel is built, which happens only when a page that extends the layout is actually rendered (so not on your API or AJAX requests). It is the way to build a menu that depends on the database, on the authenticated user or on the current locale.

PropertyTypeDescription
$menuJeroenNoten\LaravelAdminLte\Menu\BuilderThe menu builder, with the statically configured items already added
php
Event::listen(BuildingMenu::class, function (BuildingMenu $event) {
    $event->menu->add([
        'text' => 'Blog',
        'url' => 'admin/blog',
        'icon' => 'bi bi-file-earmark',
    ]);
});

The builder methods (add(), addBefore(), addAfter(), addIn(), remove() and itemKeyExists()) and the available item attributes are documented on the menu configuration page.

ReadingDarkModePreference

Dispatched when the layout is about to decide whether the panel starts in dark mode. Use it to restore a preference your application stored on its own (in a database column, for example).

PropertyTypeDescription
$darkModeJeroenNoten\LaravelAdminLte\Http\Controllers\DarkModeControllerThe controller holding the current preference

The controller exposes three methods a listener may call:

MethodDescription
enable()Start the panel in dark mode
disable()Start the panel in light mode
isEnabled()Whether dark mode is currently enabled
php
Event::listen(ReadingDarkModePreference::class, function (ReadingDarkModePreference $event) {
    if (auth()->check() && auth()->user()->prefers_dark_mode) {
        $event->darkMode->enable();
    } else {
        $event->darkMode->disable();
    }
});

IMPORTANT

This event and the next one only take part when the server side color mode toggle is in use, that is, when the color_mode.remember option is set to false. With the default remember => true, the color mode is stored by the AdminLTE v4 plugin in the browser and no request ever reaches your application. See color mode.

DarkModeWasToggled

Dispatched after the server side toggle changed the preference, so you can persist the new value.

PropertyTypeDescription
$darkModeJeroenNoten\LaravelAdminLte\Http\Controllers\DarkModeControllerThe controller, already holding the new preference
php
Event::listen(DarkModeWasToggled::class, function (DarkModeWasToggled $event) {
    auth()->user()?->update([
        'prefers_dark_mode' => $event->darkMode->isEnabled(),
    ]);
});

A complete example, with both dark mode events registered together, is available on the special menu items page.

ScreenWasLocked and ScreenWasUnlocked

Dispatched by the lockscreen controller on each transition. Both carry the same payload.

PropertyTypeDescription
$lockscreenJeroenNoten\LaravelAdminLte\Http\Controllers\LockscreenControllerThe controller that performed the transition
$userIlluminate\Contracts\Auth\Authenticatable or nullThe user whose screen was locked or unlocked
php
Event::listen(ScreenWasUnlocked::class, function (ScreenWasUnlocked $event) {
    Log::info('Screen unlocked', [
        'user' => $event->user?->getAuthIdentifier(),
    ]);
});

The controller of the payload also exposes the next public methods, useful when your application drives the lockscreen on its own:

MethodDescription
lockScreen()Flags the session as locked, without going through the route
unlockScreen()Clears the flag, without verifying a password
isLocked()Whether the session is currently locked
user()The authenticated user of the configured guard
lockscreenUrl()The url of the lockscreen page
unlockUrl()The url the unlock form submits to

Routes Registered by the Package

Two of the features above need an endpoint, so the package registers its own routes. They all live under the adminlte url prefix, carry the adminlte. route name prefix and run through the web middleware group.

Route nameMethodUriRegistered when
adminlte.darkmode.togglePOSTadminlte/darkmode/togglecolor_mode.routes is not false and disable_darkmode_routes is not true
adminlte.lockscreen.lockPOSTadminlte/lockscreen/locklockscreen.enabled is true and lockscreen.routes is not false
adminlte.lockscreen.showGETadminlte/lockscreenidem
adminlte.lockscreen.unlockPOSTadminlte/lockscreen/unlockidem

NOTE

The checks are made when the routes are registered, not inside the route files, so a compiled route file never carries a condition of its own. The compiled routes do hold whatever was registered when they were built, though: on a deployment that caches its routes, changing one of these options needs a new php artisan route:cache run, exactly like any other route change.