Joomla - 用于一个视图的组件 - 多个模型的MVC

时间:2014-05-22 12:58:41

标签: model-view-controller joomla joomla3.0

我正在用joomla 3开发一个组件,我对MVC框架并不熟悉。

该组件将管理我网站上的用户注册。 有3种不同类型的用户。 必须为每种类型显示自定义视图 - >每个用户的一个布局(Usertype1,Usertype2,Usertype3)。

关于注册,一些方法对于所有userType都是类似的,但有些是特定的(例如函数getForm())。

所以我认为最好的选择是为每个Usertype创建一个模型,然后使控制器中加载的模型适应URL中的布局:

public function display($cachable = false, $urlparams = false){

        $view = $this->getView( 'registration');
        $layout = $this->input->get('layout');
        switch ($layout) {
                case "userType1":
                    $view->setModel( $this->getModel( 'userType1' ), true );
                    $view->display();
                    break;
                case "userType2":
                    $view->setModel( $this->getModel( 'userType2' ), true );
                    $view->display();
                    break;
                case "userType3":
                    $view->setModel( $this->getModel( 'userType3' ), true );
                    $view->display();
                    break;
            }   
        parent::display();
        return $this;
    }

此代码不起作用......您认为我做出了正确的选择吗?

1 个答案:

答案 0 :(得分:0)

仅供参考,以下代码完美无缺:

在view.html.php

  public function display($tpl = null)
  {
     $document = JFactory::getDocument();
     $app = JFactory::getApplication();

     $layout = $app->input->get('layout');
     switch ($layout) {
           case "UserType1":
              $model = JModelLegacy::getInstance('userType1', 'ComponentModel');
              break;
           case "userType2":
              $model = JModelLegacy::getInstance('userType2', 'ComponentModel');
              break;
           case "userType3":
              $model = JModelLegacy::getInstance('userType3', 'ComponentModel');
              break;
     }
     $this->form = $model->getForm();

     //langage
     $lang = JFactory::getLanguage();
     $this->tag = $lang->getTag();

     parent::display($tpl);
  }

我唯一担心的是:我没有使用控制器,因此我不确定它是使用 MVC方法

的最干净方式
相关问题