The menu uses the same array shape as the PHP menu config. You compile it once per request with
that request's URL and a MenuBindings value, so one booted app can serve users with different
permissions at the same time.
Define the menu
use oxblade_adminlte::MenuBuilder;
let menu: MenuBuilder = serde_json::from_value(serde_json::json!([
"MAIN",
{ "key": "home", "text": "Home", "url": "/" },
{ "text": "Reports", "route": "reports.index", "active": ["reports*"] },
{ "text": "Users", "url": "admin/users", "can": "manage-users" },
{ "text": "Settings", "submenu": [
{ "text": "Profile", "route": ["profile.edit", { "tab": "account" }] },
{ "text": "Billing", "url": "billing", "can": ["view-billing", "manage-billing"] }
]},
{ "text": "Help", "url": "/help", "topnav_right": true }
]))?;
A plain string is a section header. Supported keys include text, header, label, url,
route, can, model, restricted, active, submenu, classes, data, icon, topnav,
topnav_right and topnav_user. For icon, use a name from your registered icon sets.
Bindings
Implement MenuBindings for a value built from the current request. Every method has a default:
| Method | Default | Use it for |
|---|---|---|
url(value) | Prefixes / to a relative path | Your URL generator |
route(name, args) | No URL | Your named routes |
authorized(ability, model) | None | Your permission checks |
translate(text) | The text unchanged | Your translations |
use oxblade_adminlte::MenuBindings;
use serde_json::Value;
struct RequestMenu<'a> {
user: Option<&'a User>,
}
impl MenuBindings for RequestMenu<'_> {
fn route(&self, name: &str, _args: &Value) -> Option<String> {
match name {
"reports.index" => Some("/reports".into()),
_ => None,
}
}
fn authorized(&self, ability: &str, _model: &Value) -> Option<bool> {
Some(self.user?.can(ability))
}
}
let compiled = menu.compile(&request_url, layout.topnav(), &RequestMenu { user });
// pass compiled.view() as "menu" to the page
compile takes the full request URL, including the query string.
Authorization
- An entry with
canis shown when an ability returnsSome(true)before any binding failure. - If
authorizedreturnsNonefor an ability, the entry is hidden. With the default bindings, restricted entries are hidden. Entries with nocan, an empty ability list, an empty string, or the PHP-falsey string"0"are unrestricted. A nonempty list containing only blank or zero names is denied. restricted: truealways hides the entry.- A submenu whose children are all hidden is hidden too.
Hiding a menu entry is not access control. Check permissions on the routes themselves.
URLs and active state
urlwins overroute. A route your bindings can't resolve gives a#link.- Only HTTP(S) URLs, rooted paths and fragments are linked; an invalid URL falls back to
#. - An entry is active when its URL or an
activepattern matches, or a child is active. An explicitfalsedoes not veto those matches. Patterns accept*wildcards and compare the query string only if the pattern has one. regex:patterns, such as"regex:~^reports/[0-9]+$~", match against the path. They use Rust'sregexsyntax, so look-around and backreferences are not available.- A parent with an active child is marked active and open.
Sections
compiled.section(name) and compiled.view() split the menu into sidebar, navbar-left,
navbar-right and navbar-user, as PHP does. Entries flagged topnav, topnav_right or
topnav_user go to the navbar. Pass layout.topnav() to compile to promote eligible sidebar
links and submenus into the left of the navbar. Within a navbar dropdown, nested groups expand
in place using a disclosure control instead of opening a separate floating dropdown.
Changing the menu in code
PHP's BuildingMenu event becomes plain method calls before you compile:
use oxblade_adminlte::{MenuBuilder, MenuEdit, MenuKey};
let extra: MenuBuilder = serde_json::from_str(r#"[{"text":"Audit log","url":"/audit"}]"#)?;
menu.edit(&MenuKey::Text("home".into()), MenuEdit::After, extra);
Before, After, Inside and Remove act on the first entry with that key, searching depth-first.
A key that isn't found changes nothing and returns false. Keys are strict: MenuKey::Integer(7)
does not match the string "7".
Escaping
Labels, translations and data values are escaped. data attribute names that aren't valid are
dropped. Menu text is never treated as HTML.