Repository: jeroennoten/Laravel-AdminLTE (language: php)

Issue:
[FEATURE] Disable Dark Mode Route
Hello,
what's the cleanest way to disable POST adminlte/darkmode/toggle route?

I don't want to use the feature in the project, so i've added

 _'layout_dark_mode' => null_

in my adminlte.php config file but the route is still available.


Discussion / hints:
@MarioVillani I think currently there is no way to disable loading of darkmode routes. I will review this on the future.

Fix patch:
diff --git a/config/adminlte.php b/config/adminlte.php
index da01ac0a..2cf5495a 100644
--- a/config/adminlte.php
+++ b/config/adminlte.php
@@ -264,6 +264,7 @@
     'password_reset_url' => 'password/reset',
     'password_email_url' => 'password/email',
     'profile_url' => false,
+    'disable_darkmode_routes' => false,
 
     /*
     |--------------------------------------------------------------------------
diff --git a/routes/web.php b/routes/web.php
index 624c5670..34f9b948 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -18,5 +18,7 @@
 // Dark Mode routes.
 //-----------------------------------------------------------------------------
 
-Route::post('/darkmode/toggle', [DarkModeController::class, 'toggle'])
-    ->name('darkmode.toggle');
+if (! config('adminlte.disable_darkmode_routes', false)) {
+    Route::post('/darkmode/toggle', [DarkModeController::class, 'toggle'])
+        ->name('darkmode.toggle');
+}


Test patch:
diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php
index 4dae482e..f176a5fc 100644
--- a/tests/ServiceProviderTest.php
+++ b/tests/ServiceProviderTest.php
@@ -1,6 +1,7 @@
 <?php
 
 use JeroenNoten\LaravelAdminLte\AdminLte;
+use JeroenNoten\LaravelAdminLte\AdminLteServiceProvider;
 
 class ServiceProviderTest extends TestCase
 {
@@ -112,10 +113,39 @@ public function testBootLoadComponents()
         $this->assertTrue(isset($aliases['adminlte-modal']));
     }
 
-    public function testBootLoadRoutes()
+    public function testBootLoadDarkModeRoutes()
     {
-        // Assert the package routes names are registered.
+        // Disable dark mode routes and check.
+
+        config(['adminlte.disable_darkmode_routes' => true]);
+        $this->clearRoutesAndReRegisterProvider();
+
+        $this->assertFalse(Route::has('adminlte.darkmode.toggle'));
+
+        // Enable dark mode routes and check again.
+
+        config(['adminlte.disable_darkmode_routes' => false]);
+        $this->clearRoutesAndReRegisterProvider();
 
         $this->assertTrue(Route::has('adminlte.darkmode.toggle'));
     }
+
+    /**
+     * Clear routes and re-register the service provider.
+     */
+    protected function clearRoutesAndReRegisterProvider()
+    {
+        // Clear the current route collection.
+
+        Route::setRoutes(new \Illuminate\Routing\RouteCollection());
+
+        // Unregister and register the provider again.
+
+        $provider = $this->app->register(AdminLteServiceProvider::class);
+        $provider->boot();
+
+        // Refresh route names after loading routes again.
+
+        Route::getRoutes()->refreshNameLookups();
+    }
 }