J2.5

Difference between revisions of "File Structure and Naming Conventions"

From Joomla! Documentation

(New page: ==Introduction== Components in Jooomla! 1.5 can now benefit from the flexibility and power of using Object Oriented Programming (OOP) practices. Most complex components will follow the Mo...)
 
m (→‎The /site folder: edited the view.html.php paragraph to define the .html section as that part of the filename.)
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Introduction==
+
__NOTOC__
Components in Jooomla! 1.5 can now benefit from the flexibility and power of using Object Oriented Programming (OOP) practices. Most complex components will follow the Model-View-Controller (MVC) design pattern. This pattern separates the data gathering (Model), presentation (View) and user interaction (Controller) activities of a component. Such separation allows for expanding or revising properties and methods of one section without requiring additional changes to the other sections. A useful set of tutorials demonstrating the process of creating an MVC component can be found [http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,jbeginners:introduction/ here].
+
Components in Jooomla! 1.5 can now benefit from the flexibility and power of using Object Oriented Programming (OOP) practices. Most complex components will follow the Model-View-Controller (MVC) design pattern. This pattern separates the data gathering (Model), presentation (View) and user interaction (Controller) activities of a component. Such separation allows for expanding or revising properties and methods of one section without requiring additional changes to the other sections. Please see [[Developing a Model-View-Controller Component - Part 1|the MVC tutorial]] for an explanations of the MVC structure.
  
Throughout this article, {ComponentName} is used to represent the name of the component. Notice also that case is important. {componentname} will refer to the lowercase version of {ComponentName}, eg. "CamelCasedController" -> "camelcasedcontroller".
+
Joomla!'s file loading system enables developers to work with separate files for controllers, views and models without worrying about importing the right file in the right place. However, for this to work, certain naming conventions need to be observed.
 +
 
 +
The Joomla! framework is extremely flexible, and it is possible to diverge from these conventions in many ways. It is nonetheless recommended to apply standard naming wherever possible to increase readability and maintainability. Additionally, the framework will take care of the many tedious class management tasks when the files and classes of a component are named correctly.
 +
 
 +
Throughout this article, {ComponentName} is used to represent the name of the component (for example, Content). Notice also that case is important. {componentname} will refer to the lowercase version of {ComponentName}, eg. <code>CamelCasedController</code> -> <code>camelcasedcontroller</code>.
 
Similarly, {ViewName} and {viewname}, {ModelName} and {modelname}, {ControllerName} and {controllername}.
 
Similarly, {ViewName} and {viewname}, {ModelName} and {modelname}, {ControllerName} and {controllername}.
  
==File Structure==
+
== Reserved words ==
 +
 
 +
There are reserved words which can't be used in names of classes and components.
 +
 
 +
An example is the word "view" (in any case) for view classes (except "view" that must be second part of that class name)<ref>Reason is in line 420 of file libraries/joomla/application/component/view.php: "<code>if (!preg_match('/View((view)*(.*(view)?.*))$/i', get_class($this), $r)) {</code>"</ref>. Because the first part of the name of view classes is the same as the controller class name, controller class names can't contain the word "view" either. And because of the conventions (although violating of it won't produce an error), controller class names must contain the component name, so component names can't contain the word "view" either. So components can't be named "com_reviews", or if they are, they must violate the naming conventions and have a different base controller class name (or have some other hacks).
 +
 
 +
== Installation package vs. actual file placement ==
  
=== Installation package vs. actual file placement ===
+
All Joomla! extensions must be packaged as a .zip installation file. According to the most common file arrangement, the package should contain at least the following two folders as a basic structure:
A zipped installation package contains these two folders:
+
* <code>site</code>
* "site"
+
* <code>admin</code>
* "admin"
+
If you set up your XML manifest file according to standard practice, the contents of <code>site</code> will be installed to <code>/components/com_{componentname}</code>, whereas the contents of <code>admin</code> will be installed to <code>/administrator/components/com_{componentname}</code>.
The contents of "site" will correspond to the folder "/components/com_{componentname}" after the package is installed.
 
The contents of "admin" will correspond to the folder "/administrator/components/com_{componentname}" after the package is installed.
 
In this article, the former naming will be used.
 
  
=== /site ===
+
== The /site folder==
 
This folder keeps the files for the frontend part of the component.
 
This folder keeps the files for the frontend part of the component.
  
:'''site/{componentname}.php'''
+
:'''<code>/site/{componentname}.php</code>'''
 
:This is the component's main file and entry point ''for the frontend part''.
 
:This is the component's main file and entry point ''for the frontend part''.
  
:'''/site/controller.php'''
+
:'''<code>/site/controller.php</code>'''
:This file holds the default frontend controller, which is a class called "{ComponentName}Controller". This class must extend the base class "JController".
+
:This file holds the default frontend controller, which is a class called <code>{ComponentName}Controller</code>. This class must extend the base class <code>JController</code>.
  
:'''/site/views'''
+
:'''<code>/site/views</code>'''
 
:This folder holds the different views for the component.
 
:This folder holds the different views for the component.
  
::'''/site/views/{viewname}'''
+
::'''<code>/site/views/{viewname}</code>'''
 
::This folder holds the files for the view {ViewName}.
 
::This folder holds the files for the view {ViewName}.
  
:::'''/site/views/{viewname}/view.html.php'''
+
:::'''<code>/site/views/{viewname}/view.html.php</code>'''
:::This file is the entry point for the view {ViewName}. It should declare the class {ComponentName}View{ViewName}. This class must extend the base class "JView".
+
:::This file is the entry point for the view {ViewName}. It should declare the class <code>{ComponentName}View{ViewName}</code>. This class must extend the base class <code>JView</code>. The <code>.html</code> section of the filename is related to the format the view will be loaded in. For example, if the <code>format</code> URL parameter is set to <code>format=feed</code>, the file <code>view.feed.php</code> will be loaded.
  
:::'''/site/views/{viewname}/tmpl'''
+
:::'''<code>/site/views/{viewname}/tmpl</code>'''
 
:::This folder holds the template files for the view {ViewName}.
 
:::This folder holds the template files for the view {ViewName}.
  
::::'''/site/views/{viewname}/tmpl/default.php'''
+
::::'''<code>/site/views/{viewname}/tmpl/default.php</code>'''
::::This is the default template for the view {ViewName}.
+
::::This is the default template for the view {ViewName}. In this PHP file, the <code>$this</code> keyword refers to the view class that the template belongs to.
  
:'''/site/models'''
+
:'''<code>/site/models</code>'''
 
:This folder holds additional models, if needed by the application.
 
:This folder holds additional models, if needed by the application.
  
::'''/site/models/{modelname}.php'''
+
::'''<code>/site/models/{modelname}.php</code>'''
::This file holds the model class {ComponentName}Model{ModelName}. This class must extend the base class "JModel". Note that the view named {ViewName} will by default load a model called {ViewName} if it exists. Most models are named after the view they are intended to be used with.
+
::This file holds the model class <code>{ComponentName}Model{ModelName}</code>. This class must extend the base class <code>JModel</code>. Note that the view named {ViewName} will by default load a model called {ViewName} if it exists. Most models are named after the view they are intended to be used with, but this is '''not''' a requirement. See [[Using multiple models in an MVC component]] for more information.
  
:'''/site/controllers'''
+
:'''<code>/site/controllers</code>'''
 
:This folder holds additional controllers, if needed by the application.
 
:This folder holds additional controllers, if needed by the application.
  
::'''/site/controllers/{controllername}.php'''
+
::'''<code>/site/controllers/{controllername}.php</code>'''
::This file holds the controller class {ComponentName}Controller{ControllerName}. This class must extend the base class "JController".
+
::This file holds the controller class <code>{ComponentName}Controller{ControllerName}</code>. This class must extend the base class <code>JController</code>.
  
 +
== The /admin folder ==
 +
The file structure is exactly the same as in the /site folder. Note that the view, models, controllers etc. of the site and admin parts are by default completely separated, and have nothing to do with each other - the site part and the admin part can be thought of as two different components! A view in the /admin folder may have a counterpart with the same name in the /site folder, yet the two views have nothing in common but their name.
  
=== /admin ===
+
However, it might sometimes make sense to share classes between the site and admin parts of the component. Especially models can be shared to avoid duplicating model code. The Joomla! framework supports this strategy. When sharing any code between site and admin applications, the classes should be designed with great care to avoid the possibility of a site user executing admin actions.
The file structure is exactly the same as in the /site folder. Note that the view, models, controllers etc. of the site and admin parts are completely separated, and have nothing to do with each other - the site part and the admin part can be thought of as two different components! A view in the /admin folder may have a counterpart with the same name in the /site folder, yet the two views have nothing in common but their name.
+
<noinclude>
<noinclude>[[Category:Development]]</noinclude>
+
----
 +
<references/>
 +
[[Category:References]][[Category:Component Development]]</noinclude>

Revision as of 12:14, 3 February 2011

The "J2.5" namespace is a namespace scheduled to be archived. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Components in Jooomla! 1.5 can now benefit from the flexibility and power of using Object Oriented Programming (OOP) practices. Most complex components will follow the Model-View-Controller (MVC) design pattern. This pattern separates the data gathering (Model), presentation (View) and user interaction (Controller) activities of a component. Such separation allows for expanding or revising properties and methods of one section without requiring additional changes to the other sections. Please see the MVC tutorial for an explanations of the MVC structure.

Joomla!'s file loading system enables developers to work with separate files for controllers, views and models without worrying about importing the right file in the right place. However, for this to work, certain naming conventions need to be observed.

The Joomla! framework is extremely flexible, and it is possible to diverge from these conventions in many ways. It is nonetheless recommended to apply standard naming wherever possible to increase readability and maintainability. Additionally, the framework will take care of the many tedious class management tasks when the files and classes of a component are named correctly.

Throughout this article, {ComponentName} is used to represent the name of the component (for example, Content). Notice also that case is important. {componentname} will refer to the lowercase version of {ComponentName}, eg. CamelCasedController -> camelcasedcontroller. Similarly, {ViewName} and {viewname}, {ModelName} and {modelname}, {ControllerName} and {controllername}.

Reserved words[edit]

There are reserved words which can't be used in names of classes and components.

An example is the word "view" (in any case) for view classes (except "view" that must be second part of that class name)[1]. Because the first part of the name of view classes is the same as the controller class name, controller class names can't contain the word "view" either. And because of the conventions (although violating of it won't produce an error), controller class names must contain the component name, so component names can't contain the word "view" either. So components can't be named "com_reviews", or if they are, they must violate the naming conventions and have a different base controller class name (or have some other hacks).

Installation package vs. actual file placement[edit]

All Joomla! extensions must be packaged as a .zip installation file. According to the most common file arrangement, the package should contain at least the following two folders as a basic structure:

  • site
  • admin

If you set up your XML manifest file according to standard practice, the contents of site will be installed to /components/com_{componentname}, whereas the contents of admin will be installed to /administrator/components/com_{componentname}.

The /site folder[edit]

This folder keeps the files for the frontend part of the component.

/site/{componentname}.php
This is the component's main file and entry point for the frontend part.
/site/controller.php
This file holds the default frontend controller, which is a class called {ComponentName}Controller. This class must extend the base class JController.
/site/views
This folder holds the different views for the component.
/site/views/{viewname}
This folder holds the files for the view {ViewName}.
/site/views/{viewname}/view.html.php
This file is the entry point for the view {ViewName}. It should declare the class {ComponentName}View{ViewName}. This class must extend the base class JView. The .html section of the filename is related to the format the view will be loaded in. For example, if the format URL parameter is set to format=feed, the file view.feed.php will be loaded.
/site/views/{viewname}/tmpl
This folder holds the template files for the view {ViewName}.
/site/views/{viewname}/tmpl/default.php
This is the default template for the view {ViewName}. In this PHP file, the $this keyword refers to the view class that the template belongs to.
/site/models
This folder holds additional models, if needed by the application.
/site/models/{modelname}.php
This file holds the model class {ComponentName}Model{ModelName}. This class must extend the base class JModel. Note that the view named {ViewName} will by default load a model called {ViewName} if it exists. Most models are named after the view they are intended to be used with, but this is not a requirement. See Using multiple models in an MVC component for more information.
/site/controllers
This folder holds additional controllers, if needed by the application.
/site/controllers/{controllername}.php
This file holds the controller class {ComponentName}Controller{ControllerName}. This class must extend the base class JController.

The /admin folder[edit]

The file structure is exactly the same as in the /site folder. Note that the view, models, controllers etc. of the site and admin parts are by default completely separated, and have nothing to do with each other - the site part and the admin part can be thought of as two different components! A view in the /admin folder may have a counterpart with the same name in the /site folder, yet the two views have nothing in common but their name.

However, it might sometimes make sense to share classes between the site and admin parts of the component. Especially models can be shared to avoid duplicating model code. The Joomla! framework supports this strategy. When sharing any code between site and admin applications, the classes should be designed with great care to avoid the possibility of a site user executing admin actions.


  1. Reason is in line 420 of file libraries/joomla/application/component/view.php: "if (!preg_match('/View((view)*(.*(view)?.*))$/i', get_class($this), $r)) {"