Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically set BackendMenu context from controller path #1311

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions modules/backend/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Config;
use Request;
use Backend;
use BackendMenu;
use Redirect;
use Response;
use Exception;
Expand Down Expand Up @@ -115,6 +116,21 @@ class Controller extends ControllerBase
*/
protected $guarded = [];

/**
* @var string Plugin author's name specified by the extending controller class in order to properly set the backend context
*/
protected $author;

/**
* @var string Plugin's name specified by the extending controller class in order to properly set the backend context
*/
protected $plugin;

/**
* @var ?string Side menu item code specified by the extending controller class in order to properly set the backend context
*/
protected $sideMenuItemCode = null;

/**
* Constructor.
*/
Expand Down Expand Up @@ -159,6 +175,8 @@ public function __construct()
}

$this->extendableConstruct();

BackendMenu::setContext("$this->author.$this->plugin", strtolower($this->plugin), $this->sideMenuItemCode);
}

public function __get($name)
Expand Down
29 changes: 20 additions & 9 deletions modules/backend/console/scaffold/controller/controller.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace {{ plugin_namespace }}\Controllers;

use BackendMenu;
use Backend\Classes\Controller;

/**
Expand All @@ -23,14 +22,26 @@ class {{ studly_name }} extends Controller
'{{ lower_author }}.{{ lower_plugin }}.{{ lower_name }}.manage_all',
];

public function __construct()
{
parent::__construct();
/**
* @var string Plugin author's name specified by the extending controller class in order to properly set the backend context
*/
protected $author = '{{ studly_author }}';

/**
* @var string Plugin's name specified by the extending controller class in order to properly set the backend context
*/
protected $plugin = '{{ studly_plugin }}';

BackendMenu::setContext('{{ plugin_code }}', '{{ lower_plugin }}', '{{ lower_name }}');
{% if sidebar %}
/**
* @var ?string Side menu item code specified by the extending controller class in order to properly set the backend context
*/
protected $sideMenuItemCode = '{{ lower_name }}';

{% if sidebar %}
/**
* @var string Body class property used for customising the layout on a controller basis.
*/
public $bodyClass = 'compact-container';
{% endif -%}

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