0

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.

  1. Is it possible to have one controller choose between 3 different views? If yes, how?
  2. 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);
    }
}
4

3 回答 3

0

如果您只需要视图,甚至不需要使用子控制器。而不是使用 task=taskname 只需使用 view=viewname 然后在 /components/com_name/views 中添加一个视图文件夹(复制现有文件夹之一)。

或者干脆跳过所有这些并使用组件创建器构建它。

于 2013-07-18T12:19:53.337 回答
0

您可以通过将以下函数添加到控制器来加载多个视图:

public function openView( $viewName ) {
        $document = \JFactory::getDocument();
        $viewType = $document->getType();
        $viewLayout = $this->input->get('layout', 'default', 'string');
        $view = $this->getView($viewName, $viewType, '', array('base_path' => $this->basePath, 'layout' => $viewLayout));

        // Get/Create the model
        if ($model = $this->getModel($viewName))
        {
            // Push the model into the view (as default)
            $view->setModel($model, true);
        }

        $view->document = $document;

        // call $view->display() to render

        return $view;
    }

然后在您的视图中,您可以加载其他视图,例如

$this->view1 = $controller->openView('view1');
$this->view2 = $controller->openView('view2');

并将其显示在模板中

<?php $this->view1->display(); ?>
<?php $this->view1->display(); ?>
于 2018-08-30T10:34:02.327 回答
0

您无需为不同的视图创建新的控制器文件。只需要复制组件的视图文件夹之一并为其提供新的视图名称。还要为该视图创建模型文件。

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

只需提供您的链接名称,例如 index.php?option=com_categories&view=listing

于 2015-10-22T12:07:24.797 回答