You are on page 1of 4

5 down vote accepted 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; }

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. down vote mvc joomla favorite asked Mar 16 '11 at 12:24 2 edited Nov 20 '11 at 20:52 share [g+] share [fb] share [tw] link|improve this question Pushparaj Joshi vikingosegundo 4711 11.7k51435 92% accept rate BTW MVC stands for Model View Controller Martin Apr 19 '11 at 8:32 feedback

2 Answers
active oldest votes 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 {

up vote 5 function display() { // gets the variable some_var if it was posted or passed view down vote GET. accepted

// default view

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

You might also like