繁体   English   中英

Joomla模型视图控制器(MVC)如何工作?

[英]How does Joomla Model View Controller (MVC) work?

我是Joomla的新手,我想知道Joomla控制器如何将数据传递给模型,模型传递给控制器​​和控制器来查看。 虽然这可能是一个愚蠢的问题,但我真的试图找到答案。 我希望我可以从stackoverflow系列中获得一些帮助。

控制器在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;
}

看看这个网站,了解如何使用Joomla的MVC制作组件和模块的详细教程。 希望能帮助到你

https://docs.joomla.org/Developing_a_MVC_Component

另请参阅官方joomla doc,了解如何使用Joomla的MVC制作组件和模块的详细教程。 希望它有所帮助http://docs.joomla.org/Developing_a_Model-View-Controller_Component/1.5/Introduction

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM