简体   繁体   English

如何在Joomla框架之外的PHP脚本中使用Joomla插件

[英]How to use Joomla plugin inside PHP script outside Joomla Framework

I have a PHP script that is executed by accessing it directly (this is for AJAX output). 我有一个PHP脚本,可以通过直接访问它来执行(这是AJAX输出的)。

I am initializing Joomla Framework variables inside that script this way: 我通过这种方式在该脚本中初始化Joomla Framework变量:

if ($JEXEC_defined==TRUE) {
    defined('_JEXEC') OR defined('_VALID_MOS') OR die( 'Restricted access' ); //security reason
    $direct_script_access=FALSE;
}

if ($JEXEC_defined==FALSE) {
    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define('JPATH_BASE', dirname(__FILE__) );
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    $direct_script_access=TRUE;

    // initialize the application 
    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();
}


if ($user->username!="") if ($direct_script_access==TRUE) {
    //PHP code when script is accessed directly
}

As an output of the script when accessed directly I need to display a Joomla Plugin, for ex: 作为直接访问时的脚本输出,我需要显示一个Joomla插件,例如:

{valsimplecalendar SRQCMPDT1 }

But instead of displaying the content of the plugin, I get a flat text "{valsimplecalendar SRQCMPDT1 }". 但是,我没有显示插件的内容,而是获得了纯文本“ {valsimplecalendar SRQCMPDT1}”。

My question: how to initialize plugin system when calling PHP directly? 我的问题:直接调用PHP时如何初始化插件系统?

Edit 编辑

I searched the web and found that I need to import the Joomla Plugins: 我在网上搜索后发现需要导入Joomla插件:

JPluginHelper::importPlugin('content');
$dispatcher = &JDispatcher::getInstance();
$dispatcher->trigger('onBeforeDisplayContent', array ( & $category, &$params, $limitstart));

But anyway that doesn't make to display plugin content when directly calling PHP script. 但是无论如何,直接调用PHP脚本时都无法显示插件内容。

This might not be what you are looking for, but I had the same problem with another Joomla plugin called FlashChart Plugin. 这可能不是您要找的东西,但是另一个名为FlashChart Plugin的Joomla插件也有同样的问题。

The plugin call looks like this: 插件调用如下所示:

{flashchart data="10,20,15,30|40,50,12,14"}Samplechart{/flashchart} {flashchart data =“ 10,20,15,30 | 40,50,12,14”} Samplechart {/ flashchart}

So I thought I could run a PHP script using a PHP plugin, calculate the data and just echo out the above string. 所以我想我可以使用PHP插件运行PHP脚本,计算数据,然后回显上述字符串。 Wrong! 错误! The curly bracket thing is preprocessed by Joomla and when you run PHP you bypass the preprocessor. 大括号是Joomla预处理的,运行PHP时会绕过预处理器。

My solution was to use the same code that the plugin used to create the charts I wanted. 我的解决方案是使用与用于创建所需图表的插件相同的代码。 I basically bypassed the plugin preprocessor and did the same thing that the plugin would have done.... which was to output the necessary code to create one of the plugin's flash charts. 我基本上绕过了插件预处理器,做了与插件一样的事情。

I was lucky though because FlashChart Plugin is very object oriented and creating the charts and outputting the swfobject code was easy. 不过我很幸运,因为FlashChart插件非常面向对象,创建图表和输出swfobject代码很容易。

In short, you may have to look in the plugin code and see what it's doing and do the same thing in your PHP code. 简而言之,您可能必须查看插件代码,查看其作用,然后在PHP代码中执行相同的操作。

Good luck. 祝好运。

Plugin events should be triggered somehow. 插件事件应该以某种方式触发。 Usually they are triggered during routing and dispatching of the app. 通常,它们是在应用程序的路由和分派期间触发的。 You can trigger any plugin methods manually using this code: 您可以使用以下代码手动触发任何插件方法:

JPluginHelper::importPlugin('system'); // Load your plug-in group
$dispatcher = JDispatcher::getInstance(); // USe JEventDispatcher for 3.x
$results = $dispatcher->trigger('onAfterInitialise'); // Trigger your custom event

More information: https://docs.joomla.org/Supporting_plugins_in_your_component 详细信息: https : //docs.joomla.org/Supporting_plugins_in_your_component

Based on your comment I think your question is really "How do I get an AJAX response without surrounding Joomla template HTML". 根据您的评论,我认为您的问题确实是“如何在不包含Joomla模板HTML的情况下获得AJAX响应”。

In this case you may want to read: 在这种情况下,您可能需要阅读:

For 1.5 对于1.5

For 1.6+ 对于1.6+

Just add at the end of your URL in your ajax call: &format=raw 只需在ajax调用中的URL末尾添加: &format = raw

Content will only be loaded without the rest! 内容将仅加载,其余部分将不加载!

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

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