dopuz8728 2012-12-17 17:21
浏览 57
已采纳

如何在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:

- 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:includes, 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!

  • 写回答

2条回答 默认 最新

  • dongshang1529 2012-12-18 09:10
    关注

    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';
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog