【问题标题】:How does Joomla Model View Controller (MVC) work?Joomla 模型视图控制器 (MVC) 如何工作?
【发布时间】:2011-07-16 13:31:21
【问题描述】:

我是 Joomla 的新手,我想知道 Joomla 控制器如何将数据传递给模型,模型传递给控制器​​,控制器传递给视图。虽然这可能是一个愚蠢的问题,但我真的试图找到答案。希望能得到stackoverflow大家庭的帮助。

【问题讨论】:

  • BTW MVC 代表模型视图控制器

标签: php model-view-controller joomla


【解决方案1】:

控制器获取 url 中的视图变量,并使用这些变量确定需要使用哪个视图。然后它设置要使用的视图。视图然后调用模型来获取它需要的数据,然后将其传递给 tmpl 以显示。

下面是这一切如何协同工作的简单设置:

components/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 );
  }

}

components/com_test/views/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();
  }

}

components/com_test/models/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(); 
  }

}

components/com_test/views/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;
}

【讨论】:

  • controller.php $var = JRequest::getVar( 'some_var' );,它从哪里得到'some_var'?是来自编码后的网址吗?
  • index.php?option=com_test&view=someview&some_var=1234
  • 所以。在 Joomla 中是与模型交互以获取数据的视图吗?
  • @MathiasLykkegaardLorenzen 这不是真的。在 Rails 中,是与模型交互并将数据传递给视图的控制器。显然视图也可以与模型交互,但这是一个不好的做法。
  • @Nobita 这取决于您所说的“交互”是什么意思。该视图向用户展示了一个 UI。它使用控制器传递的模型数据来决定这个 UI 的外观,如上面 Joomla 的案例所示。视图可以轻松地从模型中读取数据并执行与视图相关的基本操作,并且不允许修改模型。但是,在 Web 意义上,GET 和 POST 表单请求也可以看作是向控制器发送模型的视图。在这种情况下,它通常被称为视图模型,就像在MVVM(Model View ViewModel)中一样,它是基于MVC的。
【解决方案2】:

查看此站点以获取有关如何使用 Joomla 的 MVC 制作组件和模块的详细教程。希望对你有帮助

https://docs.joomla.org/Developing_a_MVC_Component

【讨论】:

  • 死链接...可能不是你发帖的时候,所以不要投反对票。
  • 每次链接到其他地方时,请同时发布链接摘要。
【解决方案3】:

另请参阅官方 joomla 文档以获取有关如何使用 Joomla 的 MVC 制作组件和模块的详细教程。希望能帮助到你 http://docs.joomla.org/Developing_a_Model-View-Controller_Component/1.5/Introduction

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2011-09-12
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多