Как работает Joomla Model View Controller (MVC)? - PullRequest
14 голосов
/ 16 марта 2011

Я новичок в Joomla, я хочу знать, как контроллер Joomla передает данные в модель, модель в контроллер и контроллер для просмотра.Хотя это может быть глупый вопрос, я действительно пытался найти ответ.Я надеюсь, что смогу получить некоторую помощь от семейства stackoverflow.

Ответы [ 3 ]

32 голосов
/ 16 марта 2011

Контроллер выбирает переменную вида в URL и использует их, чтобы определить, какой вид нужно использовать. Затем он устанавливает вид, который будет использоваться. Затем представление вызывает модель для извлечения необходимых данных, а затем передает их в tmpl для отображения.

Ниже приведена простая настройка того, как все это работает вместе:

компоненты / com_test / controller.php

class TestController extends JController
{

  // default view
  function display() {
    // gets the variable some_var if it was posted or passed view GET.
    $var = JRequest::getVar( 'some_var' );
    // sets the view to someview.html.php
    $view = & $this->getView( 'someview', 'html' );
    // sets the template to someview.php
    $viewLayout  = JRequest::getVar( 'tmpl', 'someviewtmpl' );
    // assigns the right model (someview.php) to the view
    if ($model = & $this->getModel( 'someview' )) $view->setModel( $model, true );
    // tell the view which tmpl to use 
    $view->setLayout( $viewLayout );
    // go off to the view and call the displaySomeView() method, also pass in $var variable
    $view->displaySomeView( $var );
  }

}

Компоненты / com_test / просмотров / someview / view.html.php

class EatViewSomeView extends JView
{

  function displaySomeView($var)  {
    // fetch the model assigned to this view by the controller
    $model = $this->getModel();
    // use the model to get the data we want to use on the frontend tmpl
    $data = $model->getSomeInfo($var);
    // assign model results to view tmpl
    $this->assignRef( 'data', $data );
    // call the parent class constructor in order to display the tmpl
    parent::display();
  }

}

Компоненты / com_test / модели / someview.php

class EatModelSomeView extends JModel 
{

  // fetch the info from the database
  function getSomeInfo($var) {
    // get the database object
    $db = $this->getDBO();
    // run this query
    $db->setQuery("
      SELECT 
        *
      FROM #__some_table
      WHERE column=$var
    ");
    // return the results as an array of objects which represent each row in the results set from mysql select
    return $db->loadObjectList(); 
  }

}

Компоненты / com_test / просмотров / someview / TMPL / someviewtmpl.php

// loop through the results passed to us in the tmpl
foreach($this->data as $data) {
  // each step here is a row and we can access the data in this row for each column by 
  // using $data->[col_name] where [col_name] is the name of the column you have in your db
  echo $data->column_name;
}
2 голосов
/ 16 марта 2011

Посетите этот сайт для получения подробного руководства о том, как создавать компоненты и модули с использованием MVC Joomla. Надеюсь, это поможет

https://docs.joomla.org/Developing_a_MVC_Component

1 голос
/ 06 января 2013

Также обратитесь к официальному документу Joomla для получения подробного руководства о том, как создавать компоненты и модули с использованием MVC Joomla. Надеюсь, поможет http://docs.joomla.org/Developing_a_Model-View-Controller_Component/1.5/Introduction

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...