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 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少