Joomla - how to set up a component and link MVC together

Sunday, 19 April 2009 15:43
Print
Share/Save/Bookmark
I expect you understand MVC principle.
Joomla 1.5 developers eagerly praise MVC and there are uncounted articles written on Joomla MVC. And each one of them uses different approach. Joomla MVC framework should standardise arhitecture but unfortunately even core Joomla doesn't follow the same directory order. I don't know why this is but it is confusing and frustrating. It would be nice if all components followed simple rules and structure.

I read few articles, Joomla developer section and books. As far as I learned the proper way to organizes models, views and controllers in a component should be as follows:
administrator/components
- com_examplecomponent
root of our component should be prefixed with com_
 - controllers
folder where all controllers should be located
  - index.html
- controller1.php
- controller2.php
controllers and empty index.html (for security only)
 - models
folder where all models should be located 
  - index.html
- model1.php
models and empty index.html (for security only)
  - views
views folder
  - examplecomponentfor each view separate folder should be created
default view is named the same as component
   - tmplfolder that holds layouts (HTML templates) to render the view
    - index.html
 
    - default.php
 
   - index.html
 
   - view.html.php
view class stored in a file named view.documentType.php
document types: feed, html, raw, pdf ...
 - admin.examplecomponent.php
PHP file named after the component and prefixed with admin
this is the first file to be called when our component is accessed
 - index.html
 

administration base file code example

admin.examplecomponent.php is a first accessed file and can be looked at as controller of controllers.
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

//get controller
$c = strtolower( JRequest::getCmd('c', 'defaultcontroller'));

//get the controller
require_once(JPATH_COMPONENT.DS.'controllers'.DS.$c.'.php');

$controllerName = ExamplecomponentController.ucfirst($c);
$controller = new $controllerName();
$controller->execute('display');
$controller->redirect();

?>

controller code example (how a view is linked inside controller)

<?php

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class ExamplecomponentControllerController1 extends JController
{
function display()
{
$document =& JFactory::getDocument();
$view = &$this->getView('product', $document->getType());
$view->setLayout('default');
$view->display();
}
}

?>

Joomla component - view code example (view.html.php)

<?php

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view');

class SimpleorderformViewProduct extends JView
{
function display($tpl = null)
{
$greeting = "Hello World!";
$this->assignRef( 'greet', $greeting );
parent::display($tpl);
}
}
?>

Joomla component - view code example (view.html.php)


<!-- some html stuff -->
<div>
something here :)))
<?
// echo a variable set in a view
echo $this->greet;
?>
</div>









Last Updated ( Sunday, 19 April 2009 17:15 )