include Jfactory class in an external php file, Joomla

后端 未结 3 1228
一个人的身影
一个人的身影 2020-12-08 03:15

I am writing a module for Joomla, at this point I really need to be able to connect to the database using Jfactory. Normally one could simply use $db = JFactory::getDB

相关标签:
3条回答
  • Recently, the libraries/joomla/factory.php has been removed, which was causing all the scripts that utilized require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' ); to fail. As this is still used in the accepted & most upvoted answer here, it's time to an update...

    You don't need the JDatabaseFactory class anymore to use Joomla database functions through JFactory::getDBO(); as the JFactory class provides them and gets already included.

    These five lines are enough:

    define('_JEXEC', 1);
    define('JPATH_BASE', dirname(__FILE__));
    define('DS', DIRECTORY_SEPARATOR);
    require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
    require_once JPATH_BASE.DS.'includes'.DS.'framework.php';
    

    You will be able to do e.g. this again:

    $db =& JFactory::getDBO();
    $query = "SELECT ...";
    $db->setQuery($query);
    $db->query();
    
    0 讨论(0)
  • 2020-12-08 04:05

    I'm sure that you figure it out, but maybe it would be useful for someone else

    To use joomla database class (even if you know that is not recommended :) ) you need, first to define three constants, like:

    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
    

    Then you need to include three files, like:

    require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
    require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
    require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
    $mainframe =& JFactory::getApplication('site');
    

    EDIT

    You can include only two files like:

    define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] ); // define JPATH_BASE on the external file
    require_once( JPATH_BASE . DS . 'libraries' . DS . 'import.php' ); // framework
    require_once( JPATH_BASE . DS . 'configuration.php' ); // config file
    

    Finally use joomla class, like:

    $db = JFactory::getDBO();
    
    0 讨论(0)
  • 2020-12-08 04:05

    For accessing Database information of Joomla ,You should include("configuration.php").This joomla configuaraion file contains all information of database access.

    class JFactory defined in this folder "joomlaRootFolder/libraries/joomla/factory.php"

    0 讨论(0)
提交回复
热议问题