Welcome Guest, Not a member yet? Register   Sign In
noob building menus - is there a better way?
#1

[eluser]cfraz[/eluser]
I'm converting an existing PHP spaghetti code application to CI and this is what I've done so far to build the menus.

There are 4 or 5 different menu "sets." Each page may use 1 or more menu sets in different combinations. Each menu set becomes a class instance and consists of a label to identify what kind of menu it is, and an array of menu items. Each menu item is a 2 element array of URI string and menu text.

My questions:
1. Is using a /library/class with a factory class a good way to build the menus?
2. Menu elements are defined by multi-dimensional array assignment. This works, and should be relatively easy to maintain, but it looks kinda' klugey to me. Isn't there a more elegant way to do this?

In /library/Menu_builder.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('Direct script access is not allowed.');
/**
*  Instantiate a Menu instance.
*
*  @param    string    Menu type
*/
class Menu_builder {
    function create($param)
    {
        return new Menu($param);
    }
}

/**
*  Build a menu.
*
*    Define menu $label and create an array of menu items, based on passed menu type parameter.
*    Each menu $items is a two element numeric array. Element 0 of $items is a string defining
*    controller[/function][/var1[/var2[...]]] with function and variable segments optional.
*    Element 2 of $items is a string defining menu text. If parameter $type is not one of
*    the defined menu types, $label is an empty string and $items is an empty array.
*  @param    string    Menu type
*    
*/    
class Menu {
    var $label = '';
    var $items = array();
    
    function __construct($type)
    {
        if ($type == 'category')
        {
            $this->label = 'Worksheet';
            $this->items = array(
                array('main/myp', 'My Projects'),
                array('main/req', 'Survey Request'),
                array('main/cad', 'Cadastral Surveys'),
                array('main/sup', 'Supplemental Plats'),
                array('main/min', 'Mineral Surveys'),
                array('main/oth', 'Other'),
                array('main/all', 'All Projects/Tasks'),
                array('main/arc', 'Archived Projects')
                );
        }
// more elseif clauses here

        elseif ($type == 'admin')
        {
            $this->label = 'Administration';
            $this->items = array(
                array('project/enternew', 'New Project'),
                array('duplicate', 'Duplicate A Project'),
                array('delete', 'Delete A Project'),
                array('admin/table/user', 'User'),
                array('admin/table/user_title', 'Titles'),
// more menu items here
                );
        }
    }
}
?>

In the controller:
Code:
function index()
    {
        $menus = array('category', 'report');
        $this->load->library('menu_builder');
        
        foreach ($menus as $type)
        {
            $data['menu'][] = $this->menu_builder->create($type);
        }

// more $data['whatever'] stuff
        $this->load->view('navbar', $data);
// ...
    }
}
?>
#2

[eluser]Rey Philip Regis[/eluser]
Quote:My questions:
1. Is using a /library/class with a factory class a good way to build the menus?
2. Menu elements are defined by multi-dimensional array assignment. This works, and should be relatively easy to maintain, but it looks kinda’ klugey to me. Isn’t there a more elegant way to do this?

For me building a menu which has many categories and sub categories, using factory pattern is a good implementation. As for me also using array is the most effective way of defining element in your array easy to see and understand.

Good day..
#3

[eluser]cfraz[/eluser]
Thanks, Rey. I'll just plow ahead, then.




Theme © iAndrew 2016 - Forum software by © MyBB