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.
-
BTW MVC stands for Model View Controller – Martin Apr 19 '11 at 08:32
3 Answers
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
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
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
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
// 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;
}

- 10,294
- 11
- 63
- 83
-
controller.php `$var = JRequest::getVar( 'some_var' );`, where does it get 'some_var' from? Is it from the encoded URL? – Plummer Nov 27 '12 at 17:15
-
1
-
1So. In Joomla is the view which interacts with the model to get the data? – Nobita Feb 07 '14 at 23:28
-
@Nobita in every MVC application out there, this is how it works, yes. I suggest you read about MVC itself first, as it will teach you how a proper MVC application should be built. – Mathias Lykkegaard Lorenzen Jul 24 '14 at 16:20
-
1@MathiasLykkegaardLorenzen that is not true. In Rails, is the controller which interacts with the model, and pass the data to the view. Obviously the view can interact with the model too, but that would be a bad practice. – Nobita Jul 24 '14 at 17:50
-
1@Nobita it depends on what you mean by "interacts". The view presents the user with a UI. It uses the model data that was passed on by the controller to decide how this UI looks, as seen in Joomla's case above. The view can easily **read** data from the model and perform basic view-related operations, and isn't allowed to mutate the model. However, in a web sense, GET and POST form requests can also be seen as the view sending a model to the controller. In this case, it is often called a viewmodel, like in MVVM (Model View ViewModel), which is strongly based on MVC. – Mathias Lykkegaard Lorenzen Jul 25 '14 at 14:32
-
@MathiasLykkegaardLorenzen The way I understand MVC, is that the Controller among other things, has to provide to the view the data that is needed there. If it needs more data, it will ask the Controller, not access the Model directly. I was completely ignorant on the MVVM, I will read on that :) – Nobita Jul 25 '14 at 19:28
-
We agree then. I just misinterpreted your definition of "interacts" earlier. That said though, Joomla doesn't violate this. The view doesn't interact with the model other than for reading purposes, which is perfectly normal in any MVC environment. – Mathias Lykkegaard Lorenzen Jul 25 '14 at 20:06
check out this site for detailed tutorial on how to make components and modules using Joomla's MVC. Hope it helps

- 1
- 1

- 113
- 1
- 4
-
1
-
3Every time you link somewhere else, please post a recap of the link as well. – Mathias Lykkegaard Lorenzen Jul 24 '14 at 16:19
Also refer official joomla doc for detailed tutorial on how to make components and modules using Joomla's MVC. Hope it helps http://docs.joomla.org/Developing_a_Model-View-Controller_Component/1.5/Introduction

- 11
- 1