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

Components Joomla 3.0具有多个视图的一个组件,components,joomla3.0,Components,Joomla3.0,我是Joomla的新手,我正在尝试构建一个显示项目类别的组件,当单击一个类别时,它会导致第二个视图,列出相关项目。现在,我只能让第一个视图工作。我不确定如何处理基本文件、控制器和视图文件以使第二个视图正常工作。我几天来一直在寻找答案,但都找不到任何相关的答案 我希望将其保存在单个控制器中,并根据请求的任务选择正确的视图。目前,我的请求如下: index.php?option=com_products&task=listing&cat= 总共只有3个任务,因此总共有3个视图。因此

我是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);
        }
    }
    

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


    或者干脆跳过所有这些,使用

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

    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

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

    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(); ?>
    

    我需要将类别ID从一个视图传递到另一个视图,以便正确生成第二个视图。我认为URL请求中的任何类型的信息都应该由控制器处理,而不是由视图处理。但是非常感谢你!下次使用静态视图创建组件时,我会记住这一点。