Welcome Guest, Not a member yet? Register   Sign In
Suggestion for Best practice for Menu rbac
#1

Hello guys....

In your opinion, what is the best practice for a menu class?

In CI3 i had a Menu library and in the header template i called the function build menu.

This was my library:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Menu {
    private $ci;                // for CodeIgniter Super Global Reference.
//    private $id_menu        = 'id="menu"';
//    private $class_menu        = 'class="menu"';
//    private $class_parent    = 'class="parent"';
//    private $class_last        = 'class="last"';
    // --------------------------------------------------------------------
    /**
     * PHP5        Constructor
     *
     */
    function __construct()
    {
        $this->ci =& get_instance();    // get a reference to CodeIgniter.
    }                
    
function buildMenu()
        {
            $htmlmenu "";
                    
            $htmlmenu 
.= $this->part1();
            $htmlmenu .= $this->part2();
            $htmlmenu .= $this->partAdmin();
            $htmlmenu .= "                                      </ul>";
            $htmlmenu .= $this->partFree();
            $htmlmenu .= $this->partGroup();
            $htmlmenu .= $this->partAllLogged();
            $htmlmenu .= $this->partFinal();
            
            
return $htmlmenu;
        }
    
    
function part1()
        {
                        $part1 "
    <nav class=\"navbar navbar-default navbar-fixed-top navbar-inverse\">
      <div class=\"container\">
        <div class=\"navbar-header\">
          <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">
            <span class=\"sr-only\">Toggle navigation</span>
            <span class=\"icon-bar\"></span>
            <span class=\"icon-bar\"></span>
            <span class=\"icon-bar\"></span>
          </button>
          <a class=\"navbar-brand\" href="
.base_url().">My Codeigniter</a>
        </div>
        <div id=\"navbar\" class=\"navbar-collapse collapse\">
            

          <ul class=\"nav navbar-nav\">
"
;
                        
            
return $part1;            
        
}

    function part2()
        {
            $part2 ="";
            if($this->ci->ion_auth->logged_in())
                {
$part2 "                          <p class=\"navbar-text\">".$this->ci->session->email." </p>
                                <li>"
.anchor('auth/logout'' Logout','><span class="glyphicon glyphicon-log-out"></span')."</li>
"
;
                }else
                    {
$part2 "                          <li>".anchor('auth/login'' Login','><span class="glyphicon glyphicon-log-out"></span')."</li>
"
;                    
                    
};        
                        
            
return $part2;            
        
}

    function partAdmin()
        {
            $part3 ="";
            if($this->ci->ion_auth->is_admin())
                {
$part3 "                          <li class=\"dropdown\">
                                <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">Admin <span class=\"caret\"></span></a>
                                <ul class=\"dropdown-menu\">
                                    <li>"
.anchor('example_admin''Example Admin')."</li>
                                    <li role=\"separator\" class=\"divider\"></li>
                                    <li>"
.anchor('crud''Crud Generator')."</li>
                                    <li role=\"separator\" class=\"divider\"></li>
                                    <li>"
.anchor('auth''User List')."</li>
                                    <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                    <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                    <li class=\"dropdown-submenu\">
                                        <a tabindex=\"0\">SubMenu1</a>
                                        <ul class=\"dropdown-menu\">
                                            <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                        </ul>
                                    </li>
                                    <li class=\"dropdown-submenu\">
                                        <a tabindex=\"0\">SubMenu1</a>
                                        <ul class=\"dropdown-menu\">
                                            <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                        </ul>
                                    <li role=\"separator\" class=\"divider\"></li>
                                    <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                    <li>"
.anchor('ControllerAdmin''AdminMenu1')."</li>
                                    </li>
                                </ul>
                            </li>
"
;
                }        
                        
            
return $part3;            
        
}

    function partFree()
        {
            $part4 ="";
$part4 "                          <ul class=\"nav navbar-nav navbar-right\">
                <li class=\"dropdown\">
                    <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">FreeMenu1 <span class=\"caret\"></span></a>
                    <ul class=\"dropdown-menu\">
                        <li>"
.anchor('example_guest''Example Guest')."</li>                              
                    </ul>
                </li>
"
;
        
                        
            
return $part4;            
        
}

    function partGroup()
        {
            $part5 ="";
            if($this->ci->ion_auth->in_group(2))
                {
$part5 "                  <li class=\"dropdown\">
                                <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">Only Member menu <span class=\"caret\"></span></a>
                                <ul class=\"dropdown-menu\">
                                    <li>"
.anchor('example_members''Example Member')."</li>
                                    <li role=\"separator\" class=\"divider\"></li>
                                    <li>"
.anchor('example_members''Example Member')."</li>
                                </ul>
                            </li>
"
;
                }        
                        
            
return $part5;            
        
}

    function partAllLogged()
        {
            $part6 ="";
            if($this->ci->ion_auth->logged_in())
                {
$part6 "              <li class=\"dropdown\">
                                <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">All logged menu <span class=\"caret\"></span></a>
                                <ul class=\"dropdown-menu\">
                                    <li>"
.anchor('example_logged''Example logged')."</li>
                                    <li role=\"separator\" class=\"divider\"></li>
                                    <li>"
.anchor('example_logged''Example logged')."</li>
                                </ul>
                            </li>
"
;
                }        
                        
            
return $part6;            
        
}

    function partFinal()
        {
            $part7 ="";
$part7 "                                    </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
"
;
        
                        
            
return $part7;            
        
}

        


Comment? Idea?

Thanks a lot....
Reply
#2

I’ve been thinking about making a Menu module recently. If I were to write it some things I would like it to include:
* A Menu device that implemented various Menu Handlers. Each handler would represent a front-end framework, so the same menu definitions could be ported across websites with different frontends just by changing the handler
* A component-based definition for each menu, probably building on CodeIgniter’s HTML helper
* Auto-generated menu states and breadcrumbs, especially using the handy new url_is() function
* Optional filter so the menu could be injected without having to change your View Layout
Reply
#3

A good example is a drag and drop menu manger editor that is in Boilerplate.

CodeIgniter 4 Dashboard Boilerplate
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

This is very bad practice in my opinion.
You generate html in php. You can do this. But if you do it, use the advantage of php.
One advantage is: generate dynamic output.

Another point is: a couple of functions are redundant. Why you do this?

In your case your output is static. You can do something like this:

if(loggin)
load->view('menuloggedin')
if(admin)
load->view('adminmenu')
if( ...)
load->view('...');

It is more cleaner in my opinion.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB