简体   繁体   English

如何在Joomla 2.5中呈现模块之前加载自定义类?

[英]How to load custom classes before module rendering in Joomla 2.5?

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: 我正在制作自定义库以扩展Joomla框架核心的功能,并且将所有类放置在自定义库文件夹中,如下所示:

- 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. 不幸的是,似乎Joomla在执行这些自定义库的require_once调用之前,解析了我的模板文档,读取了所有jdoc:include并存储了所有模块的输出,因为当我创建一个新模块并检查是否我的班级存在,它总是返回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! 更新 :我刚刚发现通过<jdoc:include type="modules />确实可以看到我的类存在,但是<jdoc:include type="component" />并非如此。在这种情况下,我有com_content文章,它们使用{loadposition}声明来加载模块并呈现该模块,当我这样做时,我的类就不复存在了!

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';
    }

}

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: 一种标准化的方法,并且比以前提出的解决方案简单得多的解决方案是,使用以下两种方法之一在Joomla 2.5、3.x中加载新的库:

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) ... 注册类前缀 :从12.1开始,可以根据类前缀(以前仅支持“ J”前缀,绑定到/ libraries / joomla文件夹)注册自动加载器的外观...

 // 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. 发现类 :可以使用JLoader的discover方法共同注册遵循命名约定的文件夹中的类,但不是自动装入器立即识别的类。 The discover method looks at the file names in a folder and registers classes based on those names. discover方法查看文件夹中的文件名,并基于这些文件名注册类。 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 来自: http : //developer.joomla.org/manual/ch01s04.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM