Difference between revisions of "Absolute Basics of How a Component Functions"

From Joomla! Documentation

(Added Contributors section. Removed Your controller.php File, Getting the Data, and The View sections. Reason: information varies upon installation version, and correct information is replicated inside each tutorial, which is linked in the article.)
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
+
<noinclude><languages /></noinclude>
This article is designed for Joomlaǃ beginners; it is designed to explain what a Joomlaǃ component is, and how it functions. When a specific component example will benefit the tutorial, this article will refer to an example component named Hello Worldǃ.
+
<translate><!--T:1-->
 +
This article is designed for Joomlaǃ beginners; it is designed to explain what a Joomlaǃ component is, and how it functions. When a specific component example will benefit the tutorial, this article will refer to an example component named Hello Worldǃ.</translate>
  
==What is a Joomlaǃ Component==
+
<translate>
A component is a kind of Joomla! extension. Components are the main functional units of Joomla!; they can be seen as mini-applications. An easy analogy would be that Joomla! is the operating system and the components are desktop applications. Created by a component, content is usually displayed in the center of the main content area of a template (depending on the template).
+
==What is a Joomlaǃ Component== <!--T:2-->
 +
</translate>
 +
{{Chunk:Component/<translate><!--T:58-->
 +
en</translate>}}
 +
<translate><!--T:5-->
 +
In the Joomla! framework, components can be designed using a flat model (returns HTML code for the requested page) or Model-View-Controller (herein referred to as MVC) pattern.</translate>
  
Most components have two main parts: an administrator part and a site part. The site part is what is used to render pages of your site when they are requested by your site visitors during normal site operation. The administrator part provides an interface to configure and manage different aspects of the component and is accessible through the Joomla! administrator application.
+
<translate>
 +
==Introduction to MVC== <!--T:6-->
 +
</translate>
 +
<translate><!--T:7-->
 +
MVC is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are seperate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture.</translate>
  
In the Joomla! framework, components can be designed using a flat model(returns HTML code for the requested page) or Model-View-Controller (herein referred to as MVC) pattern.
+
<translate>
 +
===Model=== <!--T:8-->
 +
</translate>
 +
<translate><!--T:9-->
 +
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 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 as system that uses a database, the model is the only element that needs to be changed, not the view or the controller.</translate>
  
==Accessing a Joomlaǃ Component==
+
<translate>
First we need to access the Joomla! platform, which is always accessed through a single point of entry:
+
===View=== <!--T:10-->
* for user access use: <tt><yoursite>/joomla/index.php<tt>
+
</translate>
* for administrator access use: <tt><yoursite>/joomla/administrator/index.php<tt>
+
<translate><!--T:11-->
 +
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 user. 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 the data received from the model.</translate>
  
Hello World! example: <tt>localhost/joomla/index.php<tt>
+
<translate>
 +
===Controller=== <!--T:12-->
 +
</translate>
 +
<translate><!--T:13-->
 +
The controller is responsible for responding to user actions. In the case of a web application, a user action is generally 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, and then pass the model into the view which displays the data.</translate>
  
To access a component, you would go to:
+
<translate>
* for user access use: <tt><yoursite>/joomla/index.php?option=com_<component_name><tt>
+
== Joomla! Component Framework Explained == <!--T:14-->
* for administrator access use: <tt><yoursite>/joomla/administrator/index.php?option=com_<component_name><tt>
+
</translate>
  
Hello World! example: <tt>localhost/joomla/index.php?option=com_helloworld<tt>
+
<translate>
 +
=== Model === <!--T:15-->
 +
</translate>
 +
<translate><!--T:16-->
 +
In the Joomla framework, models are responsible for managing the data. The first function that has to be written for a model is a get function. It returns data to the caller. For this example, the caller will be the HelloWorldViewHelloWorld view. By default, the model named HelloWorldModelHelloWorld residing in site/models/helloworld.php is the main model associated to this view.</translate>
  
==Introduction to MVC==
+
<translate><!--T:17-->
MVC is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are seperate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture.
+
So let's have a quick look at the naming conventions with an example, since the naming convention are the actual magic, that make everything work:</translate>
  
===Model===
+
<translate><!--T:18-->
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 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 as system that uses a database, the model is the only element that needs to be changed, not the view or the controller.
+
The class ''HelloWorldView'''HelloWorld''''' resides in ''site/views/'''helloworld'''/view.html.php'' and will make use of the class ''HelloWorldModel'''HelloWorld''''' in the file ''site/models/'''helloworld'''.php''</translate>
  
===View===
+
<translate><!--T:19-->
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 user. 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 the data received from the model.
+
Do let's just assume we want to use an imaginary view ''fluffy'', you would have to have:</translate>
  
===Controller===
+
<translate><!--T:20-->
The controller is responsible for responding to user actions. In the case of a web application, a user action is generally 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, and then pass the model into the view which displays the data.
+
The class ''HelloWorldView'''Fluffy''''' which resides in ''site/views/'''fluffy'''/view.html.php''. The view will make use of ''HelloWorldModel'''Fluffy''''' in the file ''site/models/'''fluffy'''.php''. Note: the actual screen of the view: ''site/views/'''fluffy'''/tmpl/default.php'' is required as well to make this example work.</translate>
  
==MVC Basic Directory Structure==
+
<translate><!--T:21-->
 +
Breaking any of these bold conventions will lead to errors or a blank page.</translate>
 +
 
 +
<translate>
 +
==Accessing a Joomlaǃ Component== <!--T:22-->
 +
</translate>
 +
<translate><!--T:23-->
 +
First we need to access the Joomla! platform, which is always accessed through a single point of entry. Using your preferred web browser, navigate to the following URL:</translate>
 +
 
 +
{| border=1
 +
| 1
 +
| <translate><!--T:24-->
 +
user access</translate>
 +
| <translate><!--T:25-->
 +
<tt><yoursite>/joomla/index.php</tt></translate>
 +
|-
 +
| 2
 +
| <translate><!--T:26-->
 +
administrator access</translate>
 +
| <translate><!--T:27-->
 +
<tt><yoursite>/joomla/administrator/index.php</tt></translate>
 +
|}
 +
 
 +
<translate><!--T:28-->
 +
Hello World! example:</translate> <tt>localhost/joomla/index.php</tt>
 +
 
 +
<translate><!--T:29-->
 +
You can use the URL of the component, or a [[#Menu| Menu]] in order to navigate to the component. In this article we will discuss using the URL.</translate>
 +
 
 +
{| border=1
 +
| 1
 +
| <translate><!--T:30-->
 +
user access</translate>
 +
| <translate><!--T:31-->
 +
<tt><yoursite>/joomla/index.php?option=com_<component_name></tt></translate>
 +
|-
 +
| 2
 +
| <translate><!--T:32-->
 +
administrator access</translate>
 +
| <translate><!--T:33-->
 +
<tt><yoursite>/joomla/administrator/index.php?option=com_<component_name></tt></translate>
 +
|}
 +
 
 +
<translate><!--T:34-->
 +
Hello World! example:</translate> <tt>localhost/joomla/index.php?option=com_helloworld</tt>
 +
 
 +
<translate>
 +
==MVC Basic Directory Structure== <!--T:35-->
 +
</translate>
 +
<translate><!--T:36-->
 
Components are stored in a directory within your Joomla! installation, specifically at:
 
Components are stored in a directory within your Joomla! installation, specifically at:
*<tt><path_to_joomla>/htdocs/components/com_<component_name>/<tt> .
+
*<tt>htdocs/<path_to_joomla>/components/com_<component_name>/</tt> .</translate>
 +
 
 +
<translate><!--T:37-->
 +
The Hello World! component would be stored in <tt> htdocs/<path_to_joomla>/components/com_helloworld/</tt>.</translate>
 +
 
 +
<translate><!--T:38-->
 +
A basic component will contain the following files within its directoryː</translate>
 +
<translate><!--T:39-->
 +
*A html file that is just a security file with a background colorː <tt>index.html</tt></translate>
 +
<translate><!--T:40-->
 +
*A php file that represents the controller itselfː <tt>controller.php</tt></translate>
 +
<translate><!--T:41-->
 +
*A php file that loads the controller classː <tt><component_name>.php</tt></translate>
 +
<translate><!--T:42-->
 +
*A php file that represents the model itselfː <tt>models/<component_name>.php</tt></translate>
 +
<translate><!--T:43-->
 +
*Another html file for background controlː <tt>models/index.html</tt></translate>
 +
<translate><!--T:44-->
 +
*A php file containing the default viewː <tt>views/<component_name>/tmpl/default.php</tt></translate>
 +
<translate><!--T:45-->
 +
*A xml file for adding a menu item typeː <tt>views/<component_name>/tmpl/default.xml</tt></translate>
 +
<translate><!--T:46-->
 +
*Another html file for background controlː <tt>views/<component_name>/tmpl/index.html</tt></translate>
 +
<translate><!--T:47-->
 +
*Another html file for background controlː <tt>views/<component_name>/index.html</tt></translate>
 +
<translate><!--T:48-->
 +
*A php file for displaying the viewː <tt>views/<component_name>/view.html.php</tt></translate>
 +
 
 +
<translate>
  
The Hello World! component would be stored in <tt> <path_to_joomla>/htdocs/components/com_helloworld/<tt>.
+
==JEXEC== <!--T:49-->
 +
</translate>
 +
<translate><!--T:50-->
 +
The following line is commonly found at the start of Joomla! PHP files:</translate>
  
A basic component will contain the following files within its directoryː
+
<source lang="php">
*A html file that allows control of the background colorː <tt>index.html<tt>
+
<?php
*A php file that represents the controller itselfː <tt>controller.php<tt>
+
    defined('_JEXEC') or die('Restricted Access');
*A php file that loads the controller classː <tt><component_name>.php<tt>
+
</source>
*A php file that represents the model itselfː <tt>models/<component_name>.php<tt>
 
*Another html file for background controlː <tt>models/index.html<tt>
 
*A php file containing the default viewː <tt>views/<component_name>/tmpl/default.php<tt>
 
*A xml file for adding a menu item typeː <tt>views/<component_name>/tmpl/default.xml<tt>
 
*Another html file for background controlː <tt>views/<component_name>/tmpl/index.html<tt>
 
*Another html file for background controlː <tt>views/<component_name>/index.html<tt>
 
*A php file for displaying the viewː <tt>views/<component_name>/view.html.php<tt>
 
  
==Tutorials on Designing a MVC Component==
+
<translate><!--T:51-->
To learn how to design your own MVC Component, please complete the tutorial for your Joomla! version.
+
This enables for a secure entry point into the Joomla! platform. [[S:MyLanguage/JEXEC| JEXEC]] contains a detailed explanation.</translate>
* version 1.5: https://docs.joomla.org/J1.5:Developing_a_MVC_Component/Introduction
 
* version 2.5: https://docs.joomla.org/J2.5:Developing_a_MVC_Component
 
* version 3.x: https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Introduction
 
  
==Contributors==
+
<translate>
* [[User:Kevinkabatra|Kevin Kabatra]]
+
==Tutorials on Designing a MVC Component== <!--T:52-->
 +
</translate>
 +
<translate><!--T:53-->
 +
To learn how to design your own MVC Component, please complete the tutorial for your Joomla! version.</translate>
 +
<translate><!--T:54-->
 +
* [[S:MyLanguage/J1.5:Developing_a_MVC_Component/Introduction| Joomla! 1.5]]</translate>
 +
<translate><!--T:55-->
 +
* [[S:MyLanguage/J2.5:Developing_a_MVC_Component| Joomla! 2.5]]</translate>
 +
<translate><!--T:56-->
 +
* [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction| Joomla! 3.x]]</translate>
  
 +
<noinclude>
 +
<translate>
 +
<!--T:57-->
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Explanations]]
 
[[Category:Explanations]]
Line 66: Line 171:
 
[[Category:Beginner Development]]
 
[[Category:Beginner Development]]
 
[[Category:Development Recommended Reading]]
 
[[Category:Development Recommended Reading]]
 +
</translate>
 +
</noinclude>

Revision as of 13:53, 4 October 2015

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Nederlands • ‎Nederlands (informeel)‎ • ‎eesti • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎português do Brasil • ‎română • ‎русский • ‎українська • ‎فارسی • ‎हिन्दी • ‎অসমীয়া • ‎বাংলা • ‎中文(台灣)‎

This article is designed for Joomlaǃ beginners; it is designed to explain what a Joomlaǃ component is, and how it functions. When a specific component example will benefit the tutorial, this article will refer to an example component named Hello Worldǃ.

What is a Joomlaǃ Component[edit]

A component is a kind of Joomla! extension. Components are the main functional units of Joomla!; they can be seen as mini-applications. An easy analogy would be that Joomla! is the operating system and the components are desktop applications. Created by a component, content is usually displayed in the center of the main content area of a template (depending on the template).

Most components have two main parts: an administrator part and a site part. The site part is what is used to render pages of your site when they are requested by your site visitors during normal site operation. The administrator part provides an interface to configure and manage different aspects of the component and is accessible through the Joomla! administrator application.

Joomla! comes with a number of core components, like the content management system, contact forms and Web Links.

See also: Module, Plugin, Template


In the Joomla! framework, components can be designed using a flat model (returns HTML code for the requested page) or Model-View-Controller (herein referred to as MVC) pattern.

Introduction to MVC[edit]

MVC is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are seperate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture.

Model[edit]

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 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 as system that uses a database, the model is the only element that needs to be changed, not the view or the controller.

View[edit]

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 user. 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 the data received from the model.

Controller[edit]

The controller is responsible for responding to user actions. In the case of a web application, a user action is generally 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, and then pass the model into the view which displays the data.

Joomla! Component Framework Explained[edit]

Model[edit]

In the Joomla framework, models are responsible for managing the data. The first function that has to be written for a model is a get function. It returns data to the caller. For this example, the caller will be the HelloWorldViewHelloWorld view. By default, the model named HelloWorldModelHelloWorld residing in site/models/helloworld.php is the main model associated to this view.

So let's have a quick look at the naming conventions with an example, since the naming convention are the actual magic, that make everything work:

The class HelloWorldViewHelloWorld resides in site/views/helloworld/view.html.php and will make use of the class HelloWorldModelHelloWorld in the file site/models/helloworld.php

Do let's just assume we want to use an imaginary view fluffy, you would have to have:

The class HelloWorldViewFluffy which resides in site/views/fluffy/view.html.php. The view will make use of HelloWorldModelFluffy in the file site/models/fluffy.php. Note: the actual screen of the view: site/views/fluffy/tmpl/default.php is required as well to make this example work.

Breaking any of these bold conventions will lead to errors or a blank page.

Accessing a Joomlaǃ Component[edit]

First we need to access the Joomla! platform, which is always accessed through a single point of entry. Using your preferred web browser, navigate to the following URL:

1 user access <yoursite>/joomla/index.php
2 administrator access <yoursite>/joomla/administrator/index.php

Hello World! example: localhost/joomla/index.php

You can use the URL of the component, or a Menu in order to navigate to the component. In this article we will discuss using the URL.

1 user access <yoursite>/joomla/index.php?option=com_<component_name>
2 administrator access <yoursite>/joomla/administrator/index.php?option=com_<component_name>

Hello World! example: localhost/joomla/index.php?option=com_helloworld

MVC Basic Directory Structure[edit]

Components are stored in a directory within your Joomla! installation, specifically at:

  • htdocs/<path_to_joomla>/components/com_<component_name>/ .

The Hello World! component would be stored in htdocs/<path_to_joomla>/components/com_helloworld/.

A basic component will contain the following files within its directoryː

  • A html file that is just a security file with a background colorː index.html
  • A php file that represents the controller itselfː controller.php
  • A php file that loads the controller classː <component_name>.php
  • A php file that represents the model itselfː models/<component_name>.php
  • Another html file for background controlː models/index.html
  • A php file containing the default viewː views/<component_name>/tmpl/default.php
  • A xml file for adding a menu item typeː views/<component_name>/tmpl/default.xml
  • Another html file for background controlː views/<component_name>/tmpl/index.html
  • Another html file for background controlː views/<component_name>/index.html
  • A php file for displaying the viewː views/<component_name>/view.html.php


JEXEC[edit]

The following line is commonly found at the start of Joomla! PHP files:

<?php
    defined('_JEXEC') or die('Restricted Access');

This enables for a secure entry point into the Joomla! platform. JEXEC contains a detailed explanation.

Tutorials on Designing a MVC Component[edit]

To learn how to design your own MVC Component, please complete the tutorial for your Joomla! version.