Reputation: 3873
I am making custom library to extend the functionality of the Joomla framework core, and I am placing all my classes in a custom library folder like so:
- libraries
- joomla
- my_custom_library
I wish for all of my classes to be present before any module rendering is performed, so that I can use them for my custom module code. Right now, the only place I can think of to load my classes in is in my template file:
<?php
// index.php in template folder
require_once(JPATH_LIBRARIES . DS . 'my_custom_library' . DS . 'Page.php';
require_once(JPATH_LIBRARIES . DS . 'my_custom_library' . DS . 'User.php';
// and so on...
// and then the HTML document follows with <jdoc:include type="modules" /> syntax
Unfortunately, it seems that Joomla parses my template document, reads all of my jdoc:include
s, and stores all of my modules' output before executing these require_once
calls for my custom library, because when I create a new module and check to see whether my class exists, it always returns false.
<?php
// mod_something_custom.php in something_custom module folder
echo (bool) class_exists('MyCustomPageClass'); // false
How can I load in all of my custom classes before anything gets rendered (in particular, my modules)? I don't wish to alter the core if possible.
UPDATE: I just found out that modules included through <jdoc:include type="modules />
do in fact see that my classes exist. However, this is not the case for <jdoc:include type="component" />
. In this case, I have com_content articles using a {loadposition}
declaration to load the module and render it. When I do this, then my classes cease to exist!
Upvotes: 1
Views: 3127
Reputation: 2304
The standardized way, and a much simpler solution than the one previously proposed, is to load new libraries in Joomla 2.5, 3.x, using either of these:
Registering a Class Prefix: Since 12.1, there is the ability to register where the auto-loader will look based on a class prefix (previously only the "J" prefix was supported, bound to the /libraries/joomla folder) ...
// Tell the auto-loader to look for classes starting with "Foo" in a specific folder.
JLoader::registerPrefix('Foo', '/path/to/custom/packages');
Discovering Classes: Classes in a folder that follow a naming convention, but not one the auto-loader immediately recognises, can be registered collectively with JLoader's discover method. The discover method looks at the file names in a folder and registers classes based on those names. Additional arguments can be used to update the class register and recurse into sub-folders.
// Register all files in the /the/path/ folder as classes with a name like:
// Prefix<Filename>
JLoader::discover('Prefix', '/the/path/');
FROM: http://developer.joomla.org/manual/ch01s04.html
Upvotes: 1
Reputation: 878
You can add this functionality by using a system plugin to include the files. Something like this should do it:
<?php
// no direct access
defined('_JEXEC') or die;
class plgSystemIncludeFiles extends JPlugin {
public function __construct(&$subject, $config = array()) {
parent::__construct($subject, $config);
}
public function onAfterRoute() {
require_once(JPATH_LIBRARIES . DS . 'my_custom_library' . DS . 'Page.php';
require_once(JPATH_LIBRARIES . DS . 'my_custom_library' . DS . 'User.php';
}
}
Upvotes: 4