Welcome Guest, Not a member yet? Register   Sign In
Over-engineering for a simple system
#1

I really feel like I'm making this harder than it needs to be, but I can't see a simpler solution... hopefully someone can help me on this.

I have a navigation sidebar (Bootstrap 4), and I want to set the navigation items as active when going to them.  Some items are in a secondary drop-down menu, and so I need to set the parent as active (menu-open) also.  So first I need to get the URI segments, and then determine if that path is under the parent and set both parent and child as active.

My problem with the method I'm using is that it uses a lot of PHP in the view, instead of separating the logic to the controller.  But I don't see a simple method of defining both paths in a controller, and then relaying that to the view for each of the menu items to be active or not active.

Admin Controller
PHP Code:
<?php

namespace App\Controllers;

class 
Admin extends Auth 
{
    public function __construct() 
    {
        parent::__construct();
        
        $uri 
= new \CodeIgniter\HTTP\URI(current_url());
        
        $this
->data['uri_segments'] = $uri->getSegments();
    }

    public function index() 
    {
        echo view('Admin/Pages/Dashboard'$this->data);
    }
    public function users($find null
    {
        $model = new \App\Models\UsersModel();
        $this->data['users'] = $model->paginate();
        $this->data['pager'] = $model->pager;

        echo view('Admin/Pages/Userlist'$this->data);
    }
    public function user($id null
    {
        $model = new \App\Models\UsersModel();
        $this->data['user'] = $model->getUser($id);

        echo view('Admin/Pages/User'$this->data);
    }


Sidebar View
PHP Code:
<nav class="mt-2">
    <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
        <li class="nav-item">
            <a href="<?= site_url('admin/profile'); ?>" class="nav-link <?= (uri_string() === 'admin/profile') ? 'active' : ''; ?>">
                <class="fas fa-user"></i>
                <p>
                    User Profile
                
</p>
            </a>
        </li>
        <li class="nav-item has-treeview <?= (in_array(implode('/',[$uri_segments[0],$uri_segments[1]]), ['admin/users', 'admin/user', 'admin/permissions', 'admin/roles'])) ? 'menu-open' : ''; ?>">
            <a href="#" class="nav-link">
                <class="nav-icon fas fa-tachometer-alt"></i>
                <p>User Management
                    
<class="right fas fa-angle-left"></i>
                </p>
            </a>
            <ul class="nav nav-treeview">
                <li class="nav-item">
                    <a href="<?= site_url('admin/users'); ?>" class="nav-link <?=(implode('/',[$uri_segments[0],$uri_segments[1]]) === 'admin/users' || 'admin/user') ? 'active' : ''; ?>">
                        <class="fas fa-users"></i>
                        <p>Users</p>
                    </a>
                </li>
            </
ul>
        </
li>
    </
ul>
</
nav
Reply


Messages In This Thread
Over-engineering for a simple system - by BilltheCat - 04-19-2020, 03:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB