Skip to content

Commit

Permalink
Automatically set BackendMenu context from controller path
Browse files Browse the repository at this point in the history
Credit to @sephyld. Replaces #1311. Closes #1269.
  • Loading branch information
LukeTowers committed Feb 26, 2025
1 parent 41f643b commit 5d0b9ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
44 changes: 35 additions & 9 deletions modules/backend/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Backend\Classes;

use Lang;
use View;
use Flash;
use Config;
use Request;
use Backend;
use Redirect;
use Response;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\View;
use Winter\Storm\Support\Facades\Flash;
use Winter\Storm\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Backend\Facades\Backend;
use Backend\Facades\BackendMenu;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Exception;
use BackendAuth;
use Backend\Facades\BackendAuth;
use Backend\Models\UserPreference;
use Backend\Models\Preference as BackendPreference;
use Backend\Widgets\MediaManager;
Expand Down Expand Up @@ -199,6 +200,26 @@ public static function __callStatic($name, $params)
return self::extendableCallStatic($name, $params);
}

/**
* Set the navigation context based on the current action & parameters
*/
protected function setNavigationContext(?string $action = null, array $params = []): void
{
$context = BackendMenu::getContext();

// @TODO: Support detecting module controllers as well
$currentClass = explode('\\', get_class($this));
$author = $currentClass[0];
$plugin = $currentClass[1];
$controller = $currentClass[count($currentClass) - 1];

$owner = $context->owner ?? "$author.$plugin";
$mainMenuCode = $context->mainMenuCode ?? strtolower($plugin);
$sideMenuCode = $context->sideMenuCode ?? strtolower($controller);

BackendMenu::setContext($owner, $mainMenuCode, $sideMenuCode);
}

/**
* Execute the controller action.
* @param string $action The action name.
Expand Down Expand Up @@ -281,6 +302,11 @@ public function run($action = null, $params = [])
BackendPreference::setAppLocale();
BackendPreference::setAppFallbackLocale();

/*
* Set the navigation context
*/
$this->setNavigationContext($action, $params);

/*
* Execute AJAX event
*/
Expand Down
7 changes: 2 additions & 5 deletions modules/backend/console/scaffold/controller/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ class {{ studly_name }} extends Controller
protected $requiredPermissions = [
'{{ lower_author }}.{{ lower_plugin }}.{{ lower_name }}.manage_all',
];
{% if sidebar %}

public function __construct()
{
parent::__construct();

BackendMenu::setContext('{{ plugin_code }}', '{{ lower_plugin }}', '{{ lower_name }}');
{% if sidebar %}

$this->bodyClass = 'compact-container';
{% endif -%}
}
{% endif -%}
}

0 comments on commit 5d0b9ca

Please sign in to comment.