CodeIgniter Forums
role based menu include - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: role based menu include (/thread-55898.html)



role based menu include - El Forum - 11-15-2012

[eluser]Unknown[/eluser]
Hi all,

Im new to all this mvc structure so just have a simple question but cant get my head around it!

Basically we have developed our business system in procedural php but now would like to port it to codeigniter.

It operates on a login session based way but the majority of pages include a menu script which is acl based on the role.

e.g. we have a two view templates html_header.php, and html_footer.php these have the header and footer obviously!. On the pages that require the menu in between these we have the menu script, it just generates a list of divs / ul's etc based on the menus they can access.

Basically what im unsure of is how do i go about doing this in ci, id like to create a view for the menu but how does the logic of this become populated, do i create a controller for this and call this from the other controllers or do i create a library or what!!

if it helps here is a little bit of code to demonstrate,
we have a sales controller, within this controller are a search and view method, search shows the results of a query from this point users can see all sales orders and click to open in a new window, at the top of the page is the menu. The view method is just a view and is opened in the new window but does not have the menu at the top. there would be many other controllers, each with their methods again some displaying a menu some not.

Code:
class Sales extends CI_Controller
{
    public function search()
    {
       $this->load->view('html_header');
       $this->load->view('menu');    /*  menu code would this be a controller, or library or .....*/
       $this->load->view('search_sales');
       $this->load->view('html_footer');
    }

    public function view()
    {
       $this->load->view('html_header');
       $this->load->view('view_sales');
       $this->load->view('html_footer');
    }

    public function edit()
    {
       $this->load->view('html_header');
       $this->load->view('menu');    /*  menu code would this be a controller, or library or .....*/
       $this->load->view('edit_sales');
       $this->load->view('html_footer');
    }

}

clearly a lot of logic is missing from these methods but i just need to understand how to get my menus over?!

any help would be greatly appreciated.

Thanks
Jamie


role based menu include - El Forum - 11-15-2012

[eluser]PhilTem[/eluser]
I'd create a library called "Navigation" that has some method called maybe "render" which you can also call via a helper "navigation_render()" so that your view only has the line

Code:
echo navigation_render();

while the helper does something like

Code:
function navigation_render()
{
  static $CI;
  isset($CI) OR $CI =& get_instance();
  
  isset($CI->navigation) OR $CI->load->library('navigation');
  
  return $CI->navigation->render();
}

What your method render of class navigation does should be up to you, but it will basically grab the allowed/viewable items of the menu and pass those to a view that will properly format the array to e.g. an UL or OL.

Besides: You might want to consider using a templating library because it'll keep your code from getting wet plus it just is nicer to have a wrapper around some content rather than pre-content, content and post-content parts, IMHO Wink


role based menu include - El Forum - 11-15-2012

[eluser]solid9[/eluser]
@philtem
And that is called a Singleton Pattern.
I just hope he understood you because he said his new.


role based menu include - El Forum - 11-16-2012

[eluser]Unknown[/eluser]
Thanks lot PhilTem, yes i understand now it would be the library that needs to be created.

Thanks again