php view controller,php – Joomla Model View Controller(MVC)如何工作?

控制器拾取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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值