Joomla 3.0一个具有多个视图的组件

问题描述 投票:0回答:3

我是Joomla的新手,我正在尝试构建一个显示项目类别的单个组件,当单击某个类别时,它会显示列出相关项目的第二个视图。现在,我只能让第一个视图工作。我不知道如何处理基本文件,控制器和视图文件以使第二个视图正常工作。我试着寻找几天的答案,但找不到任何相关的东西。

我想将它保存在一个控制器中,并根据请求的任务选择正确的视图。现在,我有这样的要求

index.php?option=com_products&task=listing&cat=

总共只有3个任务,因此总共3个视图。因此我不想打扰多个控制器。

  1. 是否有可能让一个控制器在3个不同的视图之间进行选择?如果有,怎么样?
  2. 有多个视图需要多个控制器,以保持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();

   }
}

上市\ 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
3个回答
0
投票

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

或者只是跳过所有这些并使用component creator构建它。


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


0
投票

您可以通过向Controller添加以下功能来加载多个视图:

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(); ?>
© www.soinside.com 2019 - 2024. All rights reserved.