Search code examples
phpjoomlajoomla2.5joomla-component

Using one menu in multiple views of a component in Joomla 2.5


I wrote a component in Joomla 2.5 and I use this

JSubMenuHelper::addEntry('Alpha', 'index.php?option=com_teams&task=showA');
JSubMenuHelper::addEntry('Beta', 'index.php?option=com_teams&task=showB',true);
JSubMenuHelper::addEntry('Gamma', 'index.php?option=com_teams&task=showC');
...

in each of the view.html.php files to switch from on view to another.

This works fine, but it is annoying if I add or change a menu item, because I have to change it in all the files individually.

What is the best (and conform to MVC design) way to accomplish that? I guess just simply use the php methods (include or require) isn't the way to go.


Solution

  • Instead of calling that in each of the view.html.php files, I usually call that specific code, in controller.php or controllers/*.php, so that it is kind of a "global" code for my components.

    I'm not 100% sure if it is the best way to do it in MVC, but as in some comments above, you can also use helper functions to call that code, and avoid maintaining it in multiple points.

    Although, I am pretty sure that I have found this instruction (calling in the controller) in some Joomla! tutorial, or in another Joomla! component, so it's generally a good practice so far.