home resources search newsjoinmembers: 6958
PHP Flash Java Ruby Windows Linux
joomla's picture

joomla | Wed, 2008-05-07 20:18  tags: , ,

One of the hardest things to do in any of the more popular CMS is to create and extension or module. This is because the easier it is for the end user the more complicated the code and procedure become for the programmer. If you are not the originating programmer then you have your work cut out for you. Depending on the level of documentation and the willingness of others to part with needed information it can be easy or hard. Sometimes though you just need a good "Hello world" script to get you started in the right direction. Petr Vojtechovsky wrote an excellent tutorial on how to use the new MVC framework in Joomla! to create a component.

Introduction:

I described my first "helloworld component for joomla 1.5 in my previous tutorial hello world component for joomla 1.5 I have got a huge response for joomla developers and I believe some of them were right pointing out
that helloworld should be rewritten to MVC model.

This is very brief introduction. Please refer to these links for thorough explanation.
MVC on wikipedia
The Model-View-Controller (MVC) Design Pattern for PHP by Tony Marston
developers joomla1.5 forum

MVC - model view controller

MVC is an architectural pattern used in software engineering. In complex computer applications that present lots of data to the user,
one often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface
do not impact the data handling, and that the data can be reorganized without changing the user interface.
The model-view-controller solves this problem by decoupling data access and business logic from data presentation
and user interaction, by introducing an intermediate component: the controller.

After researching various articles on the internet I came up with the following descriptions of the principles of the Model-View-Controller design pattern:

The MVC paradigm is a way of breaking an application, or even just a piece of an application's interface, into three parts:
the model, the view, and the controller.

MVC was originally developed to map the traditional input, processing, output roles into the GUI realm:

mvc model controller view joomla15

Input --> Processing --> Output
Controller --> Model --> View    

  

Model

The model is the part of the component that encapsulates the application's data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the books in the database. It will also contain methods to retrieve the list of books from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.

View

The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.

Controller

The controller is responsible for responding to user actions. In the case of a web application, a user action is a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data.

Joomla! MVC Implementation

In Joomla!, the MVC pattern is implemented using three classes:

  1. JModel
  2. JView
  3. and JController

Hello world component

a) structure

com_helloworld.xml (xml - installation source)
│   
├───admin                       - (administration folder)
│   │   index.html              - (blank file)
│   │   admin.helloworld.php    - (create controller and hand over the control)
│   │   toolbar.helloworld.php  - (tool bar definition and a 'help' menu item)
│   │   toolbar.helloworld.html.php   - (tool bar definition and a 'help' menu item)
│   │   
│   ├───images                  - (here are all picures used in the component)
│   │       helloworld.png      
│   │       index.html          - (blank file)
│   │       
│   ├───controllers             - (controllers folder)
│   │       default.php         - (default controller      - "default section")
│   │       helloagain.php      - (helloagain controller    - "helloagain section")
│   │       hellotestfoo.php    - (hellotestfoo controller - "helhellotestfooloagain sec.")
│   │       helloworld.php      - (helloworld controller   - "helloworld section")
│   │       
│   ├───models                  - (models folder)
│   │       helloworld.php      - (the only model defined here. Currently not in use)
│   │       
│   ├───views                   - (views folder)  
│   │   ├───default
│   │   │       view.php        - (default view      - "default section")
│   │   │       
│   │   ├───helloagain
│   │   │       view.php        - (helloagain view   - "helloagain section")
│   │   │       
│   │   ├───hellotestfoo
│   │   │       view.php        - (hellotestfoo view - "hellotestfoo section")
│   │   │       
│   │   └───helloworld
│   │           view.php        - (helloworld view   - "helloworld section")
│   │           
│   ├───sql                             - (SQL folder. Currently not in use. All sql files)  
│   │       uninstall.helloworld.sql      ( are commented out in the installation xml.)
│   │       install.helloworld.sql        ( The reasson is very simple. Joomla! 1.5 has )
│   │                                     ( a minor bug in installation.php. We will need )
│   │                                     ( a "night build" for the next tutorial)
│   │                                      
│   └───lang                            - (language folder)        
│           cs-CZ.com_helloworld.ini    - (czech text package)
│           en-GB.com_helloworld.ini    - (english text package)
│           
└───component
    │   index.html                      - (blank file)
    │   helloworld.php                  - (core for the frontend application. )
    │                                     ( Prints "hello world".)
    └───lang
            cs-CZ.com_helloworld.ini    - (czech text package)
            en-GB.com_helloworld.ini    - (english text package)
  

b) concept

We need to rewrite the core of our previous component.
It prints "hello world" by accessing methods of class helloScreens in admin.helloworld.html.php

 class helloScreens 
 {
  function helloworld()
    { echo JText::_('helloworld'); }
  
  function helloagain()
    { echo JText::_('helloagain'); }
  
  function hellotestfoo()
    { echo JText::_('hellotestfoo'); }
    
  function hellodefault()
    { echo JText::_('hellodefault'); }
 }      	  

All these methods will become stand-alone views. \views\helloworld\view.php

 class HelloWorldComponentView extends JView
 {

	function display()
	{
	 echo JText::_('helloworld');
	}
 }	

As you can see there is no model instance passing into the view or accessing from the view.
Unfortunatelly, there is a minor bug in joomla1.5 beta that prevents us from executing
our sql script in the installation process. We can get around this problem by installing jomla 1.5 "night build"
and I will show you how in the next joomla tutorial.

the files


Happy Publishing!

joomla's picture
Joomla! is a free, open source content management system for publishing content on the world wide web and intranets. Joomla! includes features such as page caching to improve performance, RSS feeds, printable versions of pages, news flashes, blogs, polls,
Thoughtbox - So what did you think?



 
CMS Comparison Matrix

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.