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

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

E-mail 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>









Hits: 1489
Comments (2)Add Comment
January 22, 2010     
This is very clear and informative
This is very clear and informative, but isn't there supposed to be a section for Front-end MVC?
David | 81.246.10.34
March 06, 2010     
Small mistake in file name
Your second Joomla component - view code example (view.html.php) on your mvc-structure cheat sheet should be: in tmpl/default.php.

Otherwise, thank you for the clarity of your page.
Jack | 75.182.24.12

Write comment

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

Sponsored Links

My friends

Bookingpoint
partner websites

Donate

Do you find content useful? Please donate so I can cover my hosting expenses! Thanks!