How to define JPATH_BASE in a plugin?
I want to access the joomla environment in my plugi开发者_JAVA技巧n and added these line of code (my source: 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();
But how can I ably define the JPATH_BASE variable, when I am programming a plugin (more specific: a authorisation plugin)?
I am using Joomla 1.7.
Your plugin is executed by joomla application. So you do not required to define, Just use them.
I use this for unit testing Joomla components, so my file will always be in the components folder, which I get by doing:
define('JPATH_BASE', dirname( substr(__FILE__, 0, strpos(__FILE__, 'components') )));
Here's what I put at the top of my test class to load the Joomla environment:
//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();
Note that Joomla will enable output buffering, so if your tests seem to produce no output just wait a few minutes.
精彩评论