如何在插件中定义JPATH_BASE?

时间:2011-08-08 10:48:56

标签: php joomla joomla-extensions

我想在我的插件中访问joomla环境并添加这些代码行(我的来源:http://www.diademblogs.com/content-management-systems/two-ways-to-add-joomla-users-using-your-custom-code):

 define( '_JEXEC', 1 );
 define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
 define( 'DS', DIRECTORY_SEPARATOR );

 require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
 require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

 $mainframe =& JFactory::getApplication('site');
 $mainframe->initialise();

但是,当我编写插件(更具体的:授权插件)时,如何巧妙地定义JPATH_BASE变量?

我正在使用Joomla 1.7。

2 个答案:

答案 0 :(得分:2)

您的插件由joomla应用程序执行。所以你不需要定义,只需使用它们。

答案 1 :(得分:1)

我用它来测试Joomla组件的单元,所以我的文件将始终位于components文件夹中,我可以这样做:

define('JPATH_BASE', dirname( substr(__FILE__, 0, strpos(__FILE__, 'components') )));

这是我在测试类顶部加载Joomla环境的原因:

//Load Joomla environment
if (! defined('_JEXEC'))
    define('_JEXEC', 1);
$DS=DIRECTORY_SEPARATOR;
define('DS', $DS);

//Get component path
preg_match("/\\{$DS}components\\{$DS}com_.*?\\{$DS}/", __FILE__, $matches, PREG_OFFSET_CAPTURE);
$component_path = substr(__FILE__, 0, strlen($matches[0][0]) + $matches[0][1]);
define('JPATH_COMPONENT', $component_path);

define('JPATH_BASE', substr(__FILE__, 0, strpos(__FILE__, DS.'components'.DS) ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once JPATH_BASE .DS.'includes'.DS.'framework.php';
jimport( 'joomla.environment.request' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

请注意,Joomla将启用输出缓冲,因此如果您的测试似乎没有产生输出,只需等待几分钟。