Welcome Guest, Not a member yet? Register   Sign In
Using a menu independently from specific controller
#1

[eluser]Comanche[/eluser]
Hi there,

I just realized that all my controllers do nearly the sameaction, building a menu depending on URI parameters.
They don't do exaclty the same, since some controllers are sending different parameters (for example one controller uses 'letter', one uses 'sort' etc.) but all together they nearly do the same, changing the menu depending on different parameters.

So I was wondering if it is possible to build a menu-class (or something alike) which analyzes the URI parameters and does the correct output on it's own, so that it's independent from the controllers.

At the moment the whole thing is working the following way:

1. Controller is called with specific function
2. Controller executes function and gatheres needed information
3. Controller calls private function "drawContent()", which builds the main output
4. Controller calls private function "drawMenu()", which builds the menu depending on URI Parameters
5. Controller calls $this->template->render();

"drawContent" and "drawMenu" are private function of the controller, so every controller needs a "drawMenu"-function even though this function does the same for all controllers.

What I want to have is:

1. Controller is called with specific function
2. Controller executes function and gatheres needed information
3. Controller calls private function "drawContent()", which builds the main output
4. Controller calls the "global" function "drawMenu()", which builds the menu depending on URI Parameters
5. Controller calls $this->template->render();

The idea is that I don't have "thousands" of different menu functions which I have to take care of but just one function which can take care of all different states.

Greetings,
Comanche
#2

[eluser]pistolPete[/eluser]
You can extend the Controller class:

Put a file named MY_Controller.php in /system/application/libraries with the following contents:

Code:
// choose a name for that controller subclass, e.g. "ApplicationController"
class ApplicationController extends Controller {
   function ApplicationController()
   {
        parent::Controller();
   }
    
   function drawMenu()
   {
       // build your menu here
   }
}

Now update all your existing Controllers:
Code:
class Main extends ApplicationController //instead of "extends Controller"
{
   function index()
   {
      [...]
      $this->drawContent();
      $this->drawMenu();
      $this->template->render();
   }
[...]
}
#3

[eluser]roj[/eluser]
Or just create a helper file or menu library?
#4

[eluser]roj[/eluser]
Sorry....just realised i said much the same thing as Pete. I'm really not on the ball today.
#5

[eluser]Comanche[/eluser]
Thank you very much, I didn't expect it to be that easy Smile




Theme © iAndrew 2016 - Forum software by © MyBB