Navigation Active State Resolver
Marks nav items active based on route, section, and path aliases.
Function signature
ogResolveNavActiveState(current_path, nav_items = array())
Categories
- File and Upload Safety
Parameters
current_pathCurrent route path used to mark navigation items active.nav_itemsNavigation rows with labels, URLs, sections, and optional aliases.Return value
Public-safe status string returned by the function for explicit controller branching or logging.
- success
- message
- data
Compatibility
Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.
Minimum PHP version: 7.4
Security notes
Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Marks nav items active based on route, section, and path aliases.
*
* Primary use case: Sidebar and top navigation.
* Typical inputs: current path, nav map.
* Typical output: nav map with active flags.
*
* Implementation note: Normalize paths before comparison.
*
* @param string $current_path Current request path.
* @param array $nav_items Navigation rows with path and aliases keys.
* @return array Navigation rows with active flags.
*/
function ogResolveNavActiveState($current_path, $nav_items = array()) {
$result = array(
'success' => false,
'message' => '',
'data' => array()
);
$current_path = trim((string)$current_path);
if (empty($current_path)) {
$current_path = '/';
}
$current_path = '/' . trim($current_path, '/');
if ($current_path == '/') {
$current_path = '/';
}
if (!is_array($nav_items)) {
$result['message'] = 'Navigation items must be an array.';
return $result;
}
$resolved = array();
foreach ($nav_items as $item) {
if (!is_array($item)) {
continue;
}
$path = '';
if (!empty($item['path'])) {
$path = '/' . trim((string)$item['path'], '/');
}
if ($path == '') {
$path = '/';
}
$active = false;
if ($path == $current_path) {
$active = true;
}
if (!$active && !empty($item['aliases']) && is_array($item['aliases'])) {
foreach ($item['aliases'] as $alias) {
$alias = '/' . trim((string)$alias, '/');
if ($alias == $current_path) {
$active = true;
}
}
}
$item['active'] = $active;
$resolved[] = $item;
}
$result['success'] = true;
$result['message'] = 'Navigation active state resolved.';
$result['data'] = array('items' => $resolved);
return $result;
}