Joomla core functions in external scripts

555 views
Skip to first unread message

Vytautas Civinskas

unread,
Mar 21, 2011, 3:01:02 PM3/21/11
to joomla-de...@googlegroups.com

Hi,

recently Im working on custom Joomla component. And I need Joomla core functions in some external scripts(that are not part of Joomla framework).

Let's say I have component and its custom class, that are not part of Joomla structure.

www.url.com/components/com_customcomp/custom_classes/some_class.php

So, I load Joomla classes into it:

if (!defined('JPATH_COMPONENT') or !constant('JPATH_COMPONENT')){

        define( '_JEXEC', 1 ); //let direct access

        define( 'JPATH', $_SERVER['DOCUMENT_ROOT']);
        define( 'JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . '/administrator' );
        define( 'DS', DIRECTORY_SEPARATOR );
        define('JPATH_COMPONENT', JPATH_BASE.DS.'components'.DS.'com_customcomp');

        //load joomla framework

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

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

Everything works fine except some Joomla core functions, like JURI::root(); which returns:

www.url.com/components/com_customcomp/custom_classes

Instead of:

www.url.com/

Also some strange results give JRoute::_() and etc.

What is the problem ? How to make that functions to work properly ?

Your help would be appreciated.

Ken Ballou

unread,
Mar 21, 2011, 3:33:26 PM3/21/11
to joomla-de...@googlegroups.com, vyto...@gmail.com
JURI::root() uses the result of JURI::base() and strips the final
component following the slash. In turn, JURI::base() uses the
$_SERVER['PHP_SELF'] variable (assuming you are using Apache httpd) *IF*
the "live_site" value is not set in the global configuration. In a
Joomla! site, the index.php file lives in the root directory of the
Joomla installation, so JURI::base() works as expected. However, your
URL uses a PHP file that is located in a subdirectory of your site's
root directory, so JURI::base() is "confused." (Your problem with
JRoute::_() is directly related to this confusion.)

Can you try setting "live_site" in your global configuration.php file
and see if that helps?

- Ken

Ian MacLennan

unread,
Mar 21, 2011, 3:37:11 PM3/21/11
to joomla-de...@googlegroups.com
When this question comes up I generally wonder why the external class isn't being called from within Joomla.  Is there any particular reason for this?

Regards

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


Vytautas Civinskas

unread,
Mar 21, 2011, 3:53:35 PM3/21/11
to joomla-de...@googlegroups.com
And how to do that ? Actually I don't want to make any system files modifications.

V.

2011/3/21 Ian MacLennan <ian.ma...@joomla.org>

greg keys

unread,
Mar 21, 2011, 3:54:59 PM3/21/11
to joomla-de...@googlegroups.com, Vytautas Civinskas
The mtu plugin is a great example of how to override core libraries

greg keys

unread,
Mar 21, 2011, 4:01:59 PM3/21/11
to joomla-de...@googlegroups.com, Vytautas Civinskas
If your just looking to have classes available to you I'd recommend using the JLoader feature of joomla you can build 
a neat helper class which autoloads whatever you want when it's called something along the lines of:


    function getHelpers()
    {          
   $classpath = JPATH_ROOT .DS.'components'.DS.'com_yourcomponent'.DS.'helpers';
   $hide = array('index.html', 'json.php', 'loader.php');
   
foreach( JFolder::files($classpath) as $file ) 
{
if(!in_array($file, $hide))
{
$name = ucfirst(JFile::stripExt($file));
$class = 'MYhelper' . $name;
    JLoader::register($class , $classpath.DS.$file);    
}
}
    }

Here's the api for the loader method
http://api.joomla.org/Joomla-Framework/JLoader.html
Reply all
Reply to author
Forward
0 new messages