Welcome Guest, Not a member yet? Register   Sign In
dynamic menu
#1

[eluser]huzzi[/eluser]
I'm creating a website using CI where the navigation menu is dynamically generated from the database.

What I'm going is in each page's controller I'm quering the database creating the navigation menu.

What's the best way to do this without repeating it in every controller for every page?

Many thanks in advance.

huzzi
#2

[eluser]Jamie Rumbelow[/eluser]
Use a model!

Create all the functions in the model, then just call "$this->model_name->get_menu()" or whatever in your controller.

For real DRY, create a parent controller that all other controllers inherit from. This way you can call all the code from your parent controller, and all the children controllers will have access to it.
#3

[eluser]huzzi[/eluser]
Thanks for your reply, in which model do i create the function? is there a model for the whole site like a gobal model or something?

Would you be able to show me some code about parent controller?

thanks alot for your help.



[quote author="Jemgames" date="1221174983"]Use a model!

Create all the functions in the model, then just call "$this->model_name->get_menu()" or whatever in your controller.

For real DRY, create a parent controller that all other controllers inherit from. This way you can call all the code from your parent controller, and all the children controllers will have access to it.[/quote]
#4

[eluser]xwero[/eluser]
Code:
// application/controllers/MY_Controller.php
class MY_Controller extends Controller
{
   function Frontend()
   {
      parent::Controller();
   }
}
// extending the MY_Controller class for more flexibility
class Frontend extends MY_Controller
{
   var $viewvars;

   function Frontend()
   {
      parent::MY_Controller();
      // build menu
      $this->viewvars['menu'] = $menu;
   }
}
Now all the controllers that extend the Frontend class will have the menu and you can add other variables too
Code:
class Home extend Frontend
{
   function index()
   {
       $this->viewvars['title'] = 'Welcome';
       $this->load->view('home/index',$this->viewvars);
   }
}
#5

[eluser]huzzi[/eluser]
i'll give that a try, thanks alot Smile

[quote author="xwero" date="1221229085"]
Code:
// application/controllers/MY_Controller.php
class MY_Controller extends Controller
{
   function Frontend()
   {
      parent::Controller();
   }
}
// extending the MY_Controller class for more flexibility
class Frontend extends MY_Controller
{
   var $viewvars;

   function Frontend()
   {
      parent::MY_Controller();
      // build menu
      $this->viewvars['menu'] = $menu;
   }
}
Now all the controllers that extend the Frontend class will have the menu and you can add other variables too
Code:
class Home extend Frontend
{
   function index()
   {
       $this->viewvars['title'] = 'Welcome';
       $this->load->view('home/index',$this->viewvars);
   }
}
[/quote]
#6

[eluser]huzzi[/eluser]
[quote author="xwero" date="1221229085"]
Code:
// application/controllers/MY_Controller.php
class MY_Controller extends Controller
{
   function Frontend()
   {
      parent::Controller();
   }
}
// extending the MY_Controller class for more flexibility
class Frontend extends MY_Controller
{
   var $viewvars;

   function Frontend()
   {
      parent::MY_Controller();
      // build menu
      $this->viewvars['menu'] = $menu;
   }
}
Now all the controllers that extend the Frontend class will have the menu and you can add other variables too
Code:
class Home extend Frontend
{
   function index()
   {
       $this->viewvars['title'] = 'Welcome';
       $this->load->view('home/index',$this->viewvars);
   }
}
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB