Welcome Guest, Not a member yet? Register   Sign In
Database-driven menu -- what's the best approach?
#1

[eluser]MatBeard[/eluser]
I'm new to CI, but thus far I think it's brilliant... flexible enough to get things done without having to follow conventions.

Now to my 'problem'...

I need to generate a menu structure from a database. I've got no issue with the database schema or required queries, I just don't know the best approach to getting the content onto each page.

Note, I used the words 'each page' rather than 'each view' -- I realise I can create the appropriate model and load the data into all views from all controllers, but this seems rather repetitive.

The reason I'm doing this (in case it helps with an alternative solution) is I'll be using a role-based permission system of some sort and I only want to display menus appropriate to the logged in user.

Any suggestions are much appreciated.
#2

[eluser]cbwd[/eluser]
Not 100% sure I understood what you're asking, but wouldn't you just create a helper that builds your menus for you?

If I'm on the wrong track, please be a bit more specific with the question.

Cheers
#3

[eluser]CroNiX[/eluser]
I created a simple library that assembles the login status, header, menu, footer, etc. All I do is pass it the page title and content created from my current controller to the template library and it does the rest.

In my controller I do my stuff and...
Code:
//used to hold the data that will go to the content view
$data = array();

//set the page title for the template
$page_title = 'page title';

//create the view for this method in this controller
$page_content = $this->load->view('view_file', $data, TRUE);

//now send them to the template for final output.
$this->template->make_page($page_title, $page_content);


Then in template::make_page(), something like:
Code:
public function make_page($title = '', $content = '')
{
  //pass the title to the header and output header view
  $header = $this->CI->load->view('templates/header', array('title' => $title), TRUE);

  //find out if the user is logged in or not.
  //if so it will display their profile links, etc.  If not, it shows login form
  $login_status = $this->CI->auth->current_user();

  //get menu from database and pass to menu view to assemble
  $data['menu'] = $this->CI->template_model->get_menu($login_status);
  $menu = $this->CI->load->view('templates/menu', $data, TRUE);

  //get footer
  $footer = $this->CI->load->view('templates/footer', array(), TRUE);

  //assemble and set final output (used so can use caching)
  $this->CI->output->set_output($header . $menu . $content . $footer);
}

Then you can have different methods for pages with sidebars, admin, etc. Hope that helps.
#4

[eluser]MatBeard[/eluser]
Hi CroNiX,

That looks just like the kind of thing I'm after. Thanks.

I did manage to work something out, but it involved pulling data directly from a model in the view... I didn't really want to start a project with bad habits.

I should be able to adapt your suggestion to meet my needs.

Just out of interest, what's the Auth code you're using. Is it 'home-grown' or one of the available helper classes?

Thanks again.
Mat




Theme © iAndrew 2016 - Forum software by © MyBB