gpt4 book ai didi

php - Joomla Model View Controller (MVC) 如何工作?

转载 作者:IT王子 更新时间:2023-10-29 00:00:06 24 4
gpt4 key购买 nike

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

最佳答案

Controller 在 url 中获取 View 变量,并使用这些变量确定需要使用哪个 View 。然后它设置要使用的 View 。然后 View 调用模型来获取它需要的数据,然后将其传递给要显示的 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;
}

关于php - Joomla Model View Controller (MVC) 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5325230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com