I'm new to Joomla and I'm trying to build a single component that shows categories of items, and when a category is clicked, it leads to a second view listing the relevant items. For now, I only can get the first view working. I am not sure what to do for the base file, controller, and view files to get the second view working. I tried looking for an answer for several days but couldn't find anything relevant.
I want to keep it in a single controller and choose the correct view based on the requested task. For now, I have the request as
index.php?option=com_products&task=listing&cat=
There's only going to be 3 total tasks, thus 3 total views. Therefore I didn't want to bother with multiple controllers.
- Is it possible to have one controller choose between 3 different views? If yes, how?
- Would having multiple views necessitate multiple controllers, to keep everything MVC style? If yes, how do I do that?
Structure:
com_categories
---categories.php
---controller.php
---models\categories.php
---models\listing.php
---views\categories\view.html.php
---views\categories\tmpl\default.php
---views\listing\view.html.php
---views\listing\tmpl\default.php
categories.php
$controller = JControllerLegacy::getInstance('categories');
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();
controller.php
class categoriesController extends JControllerLegacy
{
/*
* Main controller: Shows categories
* This is chosen by default.
*/
function display()
{
$view = $this->getView( 'categories', 'html' );
$view->setModel($this->getModel('categories'), true );
$view->setLayout( 'default' );
$view->display();
}
/*
* Listing controller: Shows list of items after a category is clicked
*/
function listing()
{
// This passes the category id to the model
$cat = JRequest::getVar( 'cat', '1' );
$model = $this->getModel('listing');
$model->setState('cat', $cat);
$view = $this->getView( 'listing', 'html' );
$view->setModel($model, true );
$view->setLayout( 'default' );
$view->display();
}
}
listing\view.html.php
class categoriesViewlisting extends JViewLegacy
{
function display($tpl = null)
{
$doc =& JFactory::getDocument();
// Assign data to the view
$this->item = $this->get('Products');
$this->title = $this->get('Category');
// Display the view
parent::display($tpl);
}
}