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

How does Joomla Model View Controller (MVC) work?(Joomla 模型视图控制器 (MVC) 如何工作?)
本文介绍了Joomla 模型视图控制器 (MVC) 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Joomla, I want to know how the Joomla controller passes data to the model, model to controller and controller to view. Although this might be a silly question, I really tried to find the answer. I hope I can get some help from the stackoverflow family.

推荐答案

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

The controller picks up the view variable in the url and using these determines which view needs to be used. It then sets the view to be used. The view then calls the model to fetch the data it requires and then passes this to the tmpl to be displayed.

以下是所有这些如何协同工作的简单设置:

Below is a simple setup of how this all works together:

components/com_test/controller.php

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

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

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

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;
}

这篇关于Joomla 模型视图控制器 (MVC) 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)