JFactory failing to import

我只是一个虾纸丫 提交于 2019-12-02 12:44:43

I suggest to go the official Joomla way and bootstrap an application. What I did works pretty well and is the recommended Joomla way (I haven't tested the code below but it should give you a starting point as it was a copy paste from multiple sources in my code).

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);

define('JPATH_BASE', dirname(dirname(dirname(__FILE__))));
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require JPATH_LIBRARIES.DS.'import.php';
require JPATH_LIBRARIES.DS.'cms.php';

JLoader::import('joomla.user.authentication');
JLoader::import('joomla.application.component.helper');

class MyJoomlaServer extends JApplicationWeb {

    public function doExecute() {
        $config = JFactory::getConfig();
        $email = $_POST['email'];
        $password = $_POST['password'];
        $user = ...;//get the user with the email
        file_put_contents("Log.txt", "got data: ".$email." and ".$password);
        $authenticate = JAuthentication::getInstance();
        $response = $authenticate->authenticate(array('username' => $user->username, 'password' => $password));

        return $response->status === JAuthentication::STATUS_SUCCESS;
    }

    public function isAdmin() {
        return false;
    }
}

$app = JApplicationWeb::getInstance('MyJoomlaServer');
JFactory::$application = $app;
$app->execute();

Something along the lines works for me. More platform examples can be found here https://github.com/joomla/joomla-platform-examples/tree/master/web.

try this,

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();
$db     = JFactory::getDbo();

It will work, you missed JFactory::getApplication('site');

Hope it helps..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!