Actions

J3.x

Difference between revisions of "Developing an MVC Component/Developing a Basic Component"

From Joomla! Documentation

J3.x:Developing an MVC Component
(Adds note about Github repo)
 
(40 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{:Developing a Model-View-Controller Component/3.0}}
+
<noinclude><languages /></noinclude>
 +
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 +
en</translate>}}
 +
<translate>
 +
== Notes == <!--T:21-->
 +
This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
  
== The first basic component ==
+
<!--T:22-->
Let's create a ''Hello World!'' component.
+
If you have used Joomla! before reading this tutorial, you have noticed that extensions are installed using a compressed file containing all the things which are needed for installing and uninstalling them.
=== Public display ===
+
With your favorite file manager and editor, create a file ''yoursite/components/com_helloworld/helloworld.php'' containing
+
<source lang="php">
+
Hello world
+
</source>
+
  
You can test this basic component by putting ''index.php?option=com_helloworld'' in your browser address (don't forget to prefix this address with your Joomla! 3.x installation path) after installing this component.
+
<!--T:23-->
 +
You can follow the steps below to create the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-1-basic-component.zip archive]
  
=== Administrator management ===
+
<!--T:24-->
With your favorite file manager and editor, create a file ''yoursite/administrator/components/com_helloworld/helloworld.php'' containing
+
Here are some slides based on this documents to explain how to create basic to advance Joomla! components.<br />
<source lang="php">
+
[http://bit.ly/joomlaWorkshopBasic Joomla! Basic Component Help]<br />
Hello world administration
+
[http://bit.ly/joomlaWorkshopAdvance Joomla! Advance Component Help]
</source>
+
  
You can test this basic component by putting ''administrator/index.php?option=com_helloworld'' in your browser address after installing the component.
+
== How to add a component to Joomla! == <!--T:25-->
 +
In this article we will cover how to create and install a basic Joomla! component. For this example we will be working with the Hello World! component.
  
=== Packaging an installation zip file ===
+
<!--T:26-->
If you have used Joomla before reading this tutorial, you have noticed that extensions are installed using a compressed file containing all the things which are needed for installing and uninstalling them.
+
To begin, you must first use your preferred file manager to create a directory for the Hello World! component. This directory can be anywhere on your file system, as long as it outside of your Joomla! installation directory. For this example we will name the directory <tt>com_helloworld</tt>, but this directory can be named anything.
  
With your favorite file manager, create a directory (outside your Joomla installation directory) containing
+
<!--T:27-->
* ''[[#helloworld.xml|helloworld.xml]]''
+
Next, inside this directory we need to create some files. Using your preferred file manager, create the following files; as you create the files, add the source code for each file which is found in ''[[#File Details| File Details]]''.</translate>
* ''[[#site/helloworld.php|site/helloworld.php]]''
+
* ''[[#index.html|site/index.html]]''
+
* ''[[#index.html|admin/index.html]]''
+
* ''[[#admin/helloworld.php|admin/helloworld.php]]''
+
* ''[[#index.html|admin/sql/index.html]]''
+
* ''[[#index.html|admin/sql/updates/index.html]]''
+
* ''[[#index.html|admin/sql/updates/mysql/index.html]]''
+
* ''[[#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
+
  
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/58225/com_helloworld-1.6-part01.zip archive] and install it using the extension manager of Joomla. You can test this basic component by putting ''index.php?option=com_helloworld'' or ''administrator/index.php?option=com_helloworld'' in your browser address. You can also notice that the ''Hello World!'' component is visible in the administrator site of your Joomla installation under the ''Components'' menu.
+
<!--T:28-->
 +
{| border=1
 +
| 1
 +
| ''[[#helloworld.xml|helloworld.xml]]''
 +
| <translate><!--T:35-->
 +
this is an XML (manifest) file that tells Joomla! how to install our component.</translate>
 +
|-
 +
| 2
 +
| ''[[#site/helloworld.php|site/helloworld.php]]''
 +
| <translate><!--T:36-->
 +
this is the site entry point to the Hello World! component</translate>
 +
|-
 +
| 3
 +
| ''[[#index.html|site/index.html]]''
 +
| <translate><!--T:37-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 4
 +
| ''[[#index.html|admin/index.html]]''
 +
| <translate><!--T:38-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 5
 +
| ''[[#admin/helloworld.php|admin/helloworld.php]]''
 +
| <translate><!--T:39-->
 +
this is the administrator entry point to the Hello World! component</translate>
 +
|-
 +
| 6
 +
| ''[[#index.html|admin/sql/index.html]]''
 +
| <translate><!--T:40-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 7
 +
| ''[[#index.html|admin/sql/updates/index.html]]''
 +
| <translate><!--T:54-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 8
 +
| ''[[#index.html|admin/sql/updates/mysql/index.html]]''
 +
| <translate><!--T:41-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 9
 +
| ''[[#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 +
| <translate><!--T:42-->
 +
file allowing to initialise schema version of the com_helloworld component.</translate>
 +
|}
  
==File Details==
+
<translate>
 +
=== Installing the Hello World! Component === <!--T:29-->
 +
Using your preferred file manager, create a .zip file of this directory. For this example we will name the file <tt>com_helloworld.zip</tt>, again this file could have been named anything.
 +
 
 +
<!--T:30-->
 +
Now we need to install the Hello World! component. There are two ways to do this, both are covered in [[S:MyLanguage/Installing an extension|Installing an Extension]]. Here we will cover the method using the Extension Manager of Joomla!.
 +
 
 +
<!--T:31-->
 +
*Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <tt><yoursite>/joomla/administrator/index.php</tt>. For this example we will navigate to <tt>localhost/joomla/administrator/index.php</tt>.
 +
*Click Extensions {{rarr}} Manage {{rarr}} Install {{rarr}} Upload Package File {{rarr}} Choose File
 +
*Navigate and Select File
 +
*Click Upload & Install
 +
 
 +
<!--T:32-->
 +
Note - You should see a message letting you know if the installation succeeded or failed.
 +
 
 +
<!--T:33-->
 +
You can test this basic function of the component by entering the Hello World! page for the site and administrator portions of your Joomla! website.
 +
* Using your preferred web browser, navigate to the Hello World! component page located on the site portion of your website. The address would be <tt><yoursite>/joomla/index.php?option=com_helloworld</tt>. For this example we will navigate to <tt>localhost/joomla/index.php?option=com_helloworld</tt>.
 +
* Using your preferred web browser, navigate to the Hello World! component page located on the administrator portion of your website. The address would be <tt><yoursite>/joomla/administrator/index.php?option=com_helloworld</tt>. For this example we will navigate to <tt>localhost/joomla/administrator/index.php?option=com_helloworld</tt>.
 +
 
 +
<!--T:34-->
 +
You can also notice that the ''Hello World!'' component is visible in the administrator site of your Joomla installation under the ''Components'' menu.
 +
 
 +
==File Details== <!--T:15-->
 +
</translate>
 
{{vanchor|admin/sql/updates/mysql/0.0.1.sql}}
 
{{vanchor|admin/sql/updates/mysql/0.0.1.sql}}
is an empty file allowing to initialise schema version of the com_helloworld component.
+
<translate><!--T:16-->
 +
is an empty file allowing to initialise schema version of the com_helloworld component.</translate>
 +
 
 
<source lang="sql">
 
<source lang="sql">
 
</source>
 
</source>
  
 
{{vanchor|helloworld.xml}}
 
{{vanchor|helloworld.xml}}
 +
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2" method="upgrade">
+
<extension type="component" version="3.2.0" method="upgrade">
  
 
<name>Hello World!</name>
 
<name>Hello World!</name>
Line 76: Line 143:
 
<administration>
 
<administration>
 
<!-- Administration Menu Section -->
 
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
+
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
 
<!-- Administration Main File Copy Section -->
 
<!-- Administration Main File Copy Section -->
 
<!-- Note the folder attribute: This attribute describes the folder
 
<!-- Note the folder attribute: This attribute describes the folder
Line 94: Line 161:
  
 
{{vanchor|site/helloworld.php}}
 
{{vanchor|site/helloworld.php}}
 +
 
<source lang="php">
 
<source lang="php">
 
Hello World
 
Hello World
Line 99: Line 167:
  
 
{{vanchor|admin/helloworld.php}}
 
{{vanchor|admin/helloworld.php}}
 +
 
<source lang="php">
 
<source lang="php">
 
Hello World administration
 
Hello World administration
Line 104: Line 173:
  
 
{{vanchor|index.html}}
 
{{vanchor|index.html}}
common to all folders
+
 
 +
<translate><!--T:17-->
 +
common to all folders</translate>
 +
 
 
<source lang="html4strict">
 
<source lang="html4strict">
 
<html><body bgcolor="#FFFFFF"></body></html>
 
<html><body bgcolor="#FFFFFF"></body></html>
 
</source>
 
</source>
  
{{notice|Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.}}
+
<translate>
 +
==Component Contents== <!--T:43-->
 +
</translate>
 +
<translate><!--T:44-->
 +
At this point in the tutorial, your component should contain the following files:</translate>
 +
{| border=1
 +
| 1
 +
| ''[[#helloworld.xml|helloworld.xml]]''
 +
| <translate><!--T:45-->
 +
this is an XML (manifest) file that tells Joomla! how to install our component.</translate>
 +
|-
 +
| 2
 +
| ''[[#site/helloworld.php|site/helloworld.php]]''
 +
| <translate><!--T:46-->
 +
this is the site entry point to the Hello World! component</translate>
 +
|-
 +
| 3
 +
| ''[[#index.html|site/index.html]]''
 +
| <translate><!--T:47-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 4
 +
| ''[[#index.html|admin/index.html]]''
 +
| <translate><!--T:48-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 5
 +
| ''[[#admin/helloworld.php|admin/helloworld.php]]''
 +
| <translate><!--T:49-->
 +
this is the administrator entry point to the Hello World! component</translate>
 +
|-
 +
| 6
 +
| ''[[#index.html|admin/sql/index.html]]''
 +
| <translate><!--T:50-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 7
 +
| ''[[#index.html|admin/sql/updates/index.html]]''
 +
| <translate><!--T:51-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 8
 +
| ''[[#index.html|admin/sql/updates/mysql/index.html]]''
 +
| <translate><!--T:52-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 9
 +
| ''[[#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 +
| <translate><!--T:53-->
 +
file allowing to initialise schema version of the com_helloworld component.</translate>
 +
|}
  
{{:J3.2:Developing a MVC Component/Navigate
+
{{notice|<translate><!--T:18-->
|prev=Introduction
+
Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.</translate>}}
|next=Adding a view to the site part}}
+
  
<noinclude>[[Category:Component Development]] [[Category:Joomla! 3.0]] [[Category:Joomla! 3.1]][[Category:Joomla! 3.2]]</noinclude>
+
<div class="row">
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:19-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Prev: Introduction</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:20-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part|Next: Adding a view to the site part</translate>|class=expand}}</div>
 +
</div>
 +
__NOTOC__
 +
<noinclude>
 +
<translate>
 +
<!--T:2-->
 +
[[Category:Joomla! 3.x]]
 +
[[Category:Joomla! 3.0]]
 +
[[Category:Joomla! 3.1]]
 +
[[Category:Joomla! 3.2]]
 +
[[Category:Joomla! 3.3]]
 +
[[Category:Joomla! 3.4]]
 +
[[Category:Beginner Development]]
 +
[[Category:Component Development]]
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 +
</translate>
 +
</noinclude>

Latest revision as of 12:47, 10 November 2015

Other languages:
English • ‎español • ‎français • ‎Nederlands • ‎русский
Joomla! 
3.x
Tutorial
Developing an MVC Component


This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).



Notes

This tutorial is part of the Developing a MVC Component for Joomla! 3.x tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

If you have used Joomla! before reading this tutorial, you have noticed that extensions are installed using a compressed file containing all the things which are needed for installing and uninstalling them.

You can follow the steps below to create the Hello World! component, or you can directly download the archive

Here are some slides based on this documents to explain how to create basic to advance Joomla! components.
Joomla! Basic Component Help
Joomla! Advance Component Help

How to add a component to Joomla!

In this article we will cover how to create and install a basic Joomla! component. For this example we will be working with the Hello World! component.

To begin, you must first use your preferred file manager to create a directory for the Hello World! component. This directory can be anywhere on your file system, as long as it outside of your Joomla! installation directory. For this example we will name the directory com_helloworld, but this directory can be named anything.

Next, inside this directory we need to create some files. Using your preferred file manager, create the following files; as you create the files, add the source code for each file which is found in File Details.

1 helloworld.xml this is an XML (manifest) file that tells Joomla! how to install our component.
2 site/helloworld.php this is the site entry point to the Hello World! component
3 site/index.html prevents web server from listing directory content
4 admin/index.html prevents web server from listing directory content
5 admin/helloworld.php this is the administrator entry point to the Hello World! component
6 admin/sql/index.html prevents web server from listing directory content
7 admin/sql/updates/index.html prevents web server from listing directory content
8 admin/sql/updates/mysql/index.html prevents web server from listing directory content
9 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.

Installing the Hello World! Component

Using your preferred file manager, create a .zip file of this directory. For this example we will name the file com_helloworld.zip, again this file could have been named anything.

Now we need to install the Hello World! component. There are two ways to do this, both are covered in Installing an Extension. Here we will cover the method using the Extension Manager of Joomla!.

  • Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <yoursite>/joomla/administrator/index.php. For this example we will navigate to localhost/joomla/administrator/index.php.
  • Click Extensions    Manage    Install    Upload Package File    Choose File
  • Navigate and Select File
  • Click Upload & Install

Note - You should see a message letting you know if the installation succeeded or failed.

You can test this basic function of the component by entering the Hello World! page for the site and administrator portions of your Joomla! website.

  • Using your preferred web browser, navigate to the Hello World! component page located on the site portion of your website. The address would be <yoursite>/joomla/index.php?option=com_helloworld. For this example we will navigate to localhost/joomla/index.php?option=com_helloworld.
  • Using your preferred web browser, navigate to the Hello World! component page located on the administrator portion of your website. The address would be <yoursite>/joomla/administrator/index.php?option=com_helloworld. For this example we will navigate to localhost/joomla/administrator/index.php?option=com_helloworld.

You can also notice that the Hello World! component is visible in the administrator site of your Joomla installation under the Components menu.

File Details

admin/sql/updates/mysql/0.0.1.sql is an empty file allowing to initialise schema version of the com_helloworld component.

 

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">
 
	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>December 2013</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.1</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>
 
	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>
 
	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
	</files>
 
	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld'>Hello World!</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
		</files>
	</administration>
 
</extension>

site/helloworld.php

Hello World

admin/helloworld.php

Hello World administration

index.html

common to all folders

<html><body bgcolor="#FFFFFF"></body></html>

Component Contents

At this point in the tutorial, your component should contain the following files:

1 helloworld.xml this is an XML (manifest) file that tells Joomla! how to install our component.
2 site/helloworld.php this is the site entry point to the Hello World! component
3 site/index.html prevents web server from listing directory content
4 admin/index.html prevents web server from listing directory content
5 admin/helloworld.php this is the administrator entry point to the Hello World! component
6 admin/sql/index.html prevents web server from listing directory content
7 admin/sql/updates/index.html prevents web server from listing directory content
8 admin/sql/updates/mysql/index.html prevents web server from listing directory content
9 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.
Info non-talk.png
General Information

Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.