Rajat
Rajat

Reputation: 323

Proper use of MVC to display front-end in the Joomla controller

I am developing a Joomla component and I have many tasks to be performed in the components. What I am doing presently is Having a single view and the controller corresponding it. Now if a user requests some task, I simply call corresponding method in the controller. In this method itself I have written all the HTML code which is display to the user. Everything is working properly.

My only doubt is, am I still following MVC model and will my component be accepted by Joomla community?

If this isn't the proper way, please tell me how can I set different views for different tasks from a single controller. View are in views>name>tmpl>default.php and view>name>tmpl>sample.php

Upvotes: 2

Views: 513

Answers (2)

Craig
Craig

Reputation: 9330

This appears to require a multi-part answer, so,

  1. The JED "Community" has several good documents to help you with submitting content these include a Terms of Service, an Extensions Directory FAQ and a blog entry about the "Enhanced Approval Process" that started in late 2010.

  2. As far as the "community" is probably concerned the "correct" way to handle different views from the same controller is to make sure that end users/designers can user Joomla!'s overrides. So each file in your tmpl directory should try to have only HTML and multiple <?php echo $this->myVariable; ?> in it where possible, with the occasional loop to unravel an array of data for display. Your controller shouldn't be generating the HTML, if it does then end users/designers won't be able to create a template override for you component.

  3. If you look at the standard Joomla! 2.5 components the arrangement of Controllers and sub-controller tends to match the view, e.g. if you look at com_content there is a corresponding controller and view for articles, article and featured.

  4. Having said that, it is acceptable, and supported to have different tmpl for the same view dependent on the task a user is performing. This is accomplished by using the controller.task form of the calling URL. e.g. one of our components presents the same view but based on the users permissions they may be looking at the edit tmpl or the run tmpl file. The code to achieve this is quite simple.

e.g.

if($canDo->get('core.edit')) {
    $plan = '<a href="'.JRoute::_( 'index.php?option=com_easystaging&task=plan.edit&id='. $row->id ).'">'.$row->name.'</a>';
} elseif ($canDo->get('easystaging.run')) {
    $plan = '<a href="'.JRoute::_( 'index.php?option=com_easystaging&task=plan.run&id='. $row->id ).'">'.$row->name.'</a>';
} else {
    $plan = $row->name;
}

Upvotes: 1

GDP
GDP

Reputation: 8178

Was not going to initially answer this, but "accepted by Joomla community" convinced me to throw in my two-bits.

IMHO, the typical Joomla user is not an expert in MVC, PHP, or even development for that matter, so my first concern is to make it "acceptable" to them. I try to model any component development after a commonly accepted component like the Joomla Content component, and how the user interacts with it. When you add a new menu item for an Article, notice how the choices are organized - Archived, Single, Category, Featured, etc.. This is the organization that a user understands, so I organize my MVC accordingly

Perhaps not a technically perfect answer, but at the end of the day, the Joomla CMS is typically for non-technical users, so I usually look at it from their standpoint first, making sure I stay technically sound.

Upvotes: 1

Related Questions