【发布时间】:2025-12-31 16:35:01
【问题描述】:
我是 Joomla 的新手,我正在尝试构建一个显示项目类别的单个组件,当单击一个类别时,它会导致列出相关项目的第二个视图。目前,我只能让第一个视图正常工作。我不确定如何处理基本文件、控制器和视图文件以使第二个视图正常工作。我尝试了几天寻找答案,但找不到任何相关内容。
我想将它保存在单个控制器中,并根据请求的任务选择正确的视图。目前,我的请求为
index.php?option=com_products&task=listing&cat=
总共只有 3 个任务,因此总共有 3 个视图。因此我不想打扰多个控制器。
- 是否可以让一个控制器在 3 个不同的视图之间进行选择?如果是,怎么做?
- 拥有多个视图是否需要多个控制器才能保持 MVC 风格?如果是,我该怎么做?
结构:
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);
}
}
【问题讨论】:
标签: components joomla3.0