0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
jost21
  • 1,084
  • 3
  • 15
  • 29
  • Typically you would see something like this go in a helper file and then just have each view call the helper's method which actually outlines the menu like above. – David Fritsch Sep 25 '13 at 04:24

1 Answers1

2

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.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – NeverHopeless Sep 25 '13 at 05:59
  • The question is: "I am writing this code in the view. But I don't want to write it in all views. What is the best (and conform to MVC design) way to accomplish that?". Simple answer is "move your code to the controller". It's not a clarification request. – mavrosxristoforos Sep 25 '13 at 06:01
  • It seems like a comment than an answer. Please add more relevant bits. – NeverHopeless Sep 25 '13 at 06:05
  • I hope it looks more like an answer now :) – mavrosxristoforos Sep 25 '13 at 06:10
  • I think I found the mentioned tutorial (J2.5:Developing a MVC Component/Adding categories) and I tried to do that. I got a file named like the component (without com_) in the helpers folder. The teams.php contains the abstract class teamsHelper (with the public static function addSubmenu($submenu) ) I (try to) call the function with teamsHelper::addSubmenu('alpha') in the controller but get the fatal error "Class 'teamsHelper' not found in ..." – jost21 Sep 26 '13 at 04:15
  • My bad, I missed the "JLoader::register('teamsHelper', ..." in the main file – jost21 Sep 26 '13 at 04:31
  • I'm glad you found it! – mavrosxristoforos Sep 26 '13 at 04:45