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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀