Sign up to our newsletter to receive notifications of extension updates.

Friday, 07 January 2011 21:26

A short note about JPATH_COMPONENT

Written by 
A short note about JPATH_COMPONENT http://www.everaldo.com/
When developing components it is often useful to use constant references to refer to a particular file or folder. To do this we can use the JPath class and one of the many constants provided. For example, these two constants will give you the following results:

JPATH_COMPONENT -- Sets the component path /Joomla/components/com_example
JPATH_COMPONENT_ADMINISTRATOR -- sets the backend component path /Joomla/administrator/components/com_example

You can check out this page for the full list of JPath constants.

These are obviously useful as they stop you having to type out long paths every time you want to refer to your working component folder. However, there is another factor which you may wish to consider.


First of all, ask yourself this question. Are you creating a class whose functions are likely to ever be used by another component? If this is the case, you should not use any JPATH_COMPONENT references. This is because the second component (let's call it com_example2) will cause all instances of JPATH_COMPONENT to refer to the com_example2 folder. Therefore, any references to files or folders in the original class will be wrong and requires will fail.
The best way to avoid these errors and to make your classes easy to use with other components is to use the JPATH_SITE and JPATH_ADMINISTRATOR constants instead. eg.
$file1 = JPATH_SITE.'/com_example/classes/someclass1.php;
$file2 = JPATH_ADMINISTRATOR.'/com_example/classes/someclass2.php;
require_once($file1);
require_once($file2);

After you have done this your classes will be available to any other extension without causing any errors.
Read 5418 times Last modified on Friday, 07 January 2011 22:19

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.