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

2024-05-08

我是 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/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();
  }

}

组件/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(); 
  }

}

组件/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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Joomla 模型视图控制器 (MVC) 如何工作? 的相关文章

随机推荐

  • 如何使用 Apple Map Kit 实现地址自动完成

    我想自动填写用户的地址 与 google api 在此链接中提供的地址相同 https developers google com maps documentation javascript places autocomplete hl e
  • 以不同顺序对多列上的结构化 Numpy 数组进行排序

    我有一个结构化的 numpy 数组 dtype price float counter int values 35 1 36 2 36 3 a np array values dtype dtype 我想按价格排序 如果价格相等则按计数器排
  • 如何删除导航视图不必要的顶部填充?

    标题和图中显示的第一个项目之间有不必要的顶部填充 如何将其去除 你可以在这里找到源代码 https github com chrisbanes cheesesquare https github com chrisbanes cheeses
  • $mysqli->fetch_object($result) 不起作用

    我正在学习mysqli 我正在尝试从表 tbllogin 中获取数据 DATABASE CONNECTION hostname p localhost database dbLogin username user1 password pwd
  • ansible 用户模块总是显示已更改

    我正在努力正确使用 ansible 的用户模块 问题是每次我运行我的剧本时 我创建的用户always显示为已更改 即使我已经创建了它们 我在这里发现其他人也有同样的问题 https github com ansible ansible is
  • 不重新渲染

    我正在尝试在我的应用程序 Seam RichFaces 中显示购物车 并包含 从购物车中删除
  • 如何禁用页眉和页脚 Selenium 打印

    有谁知道如何在硒中打印时禁用 页眉和页脚 选项 默认情况下设置为 true 有人知道如何解决这个问题吗 谢谢你 import json import os from selenium import webdriver setting htm
  • nodejs googleapis,authClient.request 不是函数

    我正在像这样的一个函数中创建一个 oauth2client 并返回它 实际上 我确实传递了客户端 ID 秘密 重定向 URL 和凭据 据我检查 这些都是正确的 var OAuth2 google auth OAuth2 var oauth2
  • java.lang.IllegalArgumentException:预期唯一结果或 null,但得到多个! - Spring Data Mongo

    我在用着Spring Boot v2 2 2 RELEASE and Spring Data MongoDB 在此示例中 我正在查找按部门代码执行组并获取该组下的所有员工 样本数据 firstName Laxmi lastName Para
  • 以有效的方式从 BigQuery 读取到 Spark 中?

    使用时BigQuery 连接器 https cloud google com hadoop examples bigquery connector spark example要从 BigQuery 读取数据 我发现它首先将所有数据复制到 G
  • 在 Matlab 中快速加载大块二进制文件

    我有一些相当大的 int16 格式的数据文件 256 个通道 大约 75 1 亿个样本 每个文件约 40 50 GB 左右 它以平面二进制格式编写 因此结构类似于 CH1S1 CH2S1 CH3S1 CH256S1 CH1S2 CH2S2
  • 如何转置 3D np 数组中的每个元素

    给定一个 3D 数组 a 我想对其第一个索引中的每个元素调用 np transpose 例如 给定数组 array 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3
  • 光标 .svg 在 Chrome 61.0.3163.100 中不起作用

    我对这个 css 有疑问 我都尝试过auto and default但我仍然看到默认光标 cursor url img extra arrow next svg auto cursor url img extra arrow next sv
  • 使用 Firebase orderByChild 进行不区分大小写的排序

    我正在使用 FirebaseRecyclerAdapter 来显示字符串列表 我的查询使用 orderByChild 结果首先按大写字母排序 如下所示 项目 1 项目 2 项目 3 aItem bItem zItem 如何使用 orderB
  • SQLAlchemy - 连接表关系上的 order_by

    我正在使用声明式 SQLAlchemy 并且有三个模型 Role Permission and RolePermission 在我的Role模型 我有以下内容 class Role Base name Column u NAME VARCH
  • Axios 与 Superagent

    如果我使用Axios https github com mzabriskie axios and 超级经纪人 https github com visionmedia superagent为了依次调用同一个 api 在这两种情况下 我都会首
  • 刷新后,socket.io 客户端多次侦听同一事件

    我得到了一个包含项目表的母版页 成功后表数据将不断刷新socket io与服务器的连接 单击表中的某个项目 该项目的 id 将传递到服务器 时 将使用 ajax 加载子视图 并通过侦听来自服务器的事件不断刷新该子视图 现在的问题是 通过选择
  • D3:如何在Groups of Force布局节点上绘制多个凸包?

    我试图在力布局中的所有组上绘制凸包 但我只画出了一半的凸包 当 D3 尝试绘制其余的船体时 控制台返回错误 元素尚未创建 然而 当我检查控制台中的 groups 变量时 所有组数据都在那里 x y 数据都设置得很好 见下图 我什至尝试在ti
  • 在应用程序窗口外检测 StylusDown

    简单的问题 希望答案不会是 你不能 我如何 在代码中 订阅 全局 手写笔按下事件 Windows 7 显然以某种方式做到了这一点 因为只要我使用手写笔 wacomm 笔和触摸 但这似乎无关紧要 就会出现小平板电脑图标 我想创建一个简单的绘图
  • Joomla 模型视图控制器 (MVC) 如何工作?

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