JFactory failing to import

2019-03-03 21:21发布

I am trying to make a login system for an android application that works in with my 2.5 Joomla website. I am trying to do this by making a Joomla plugin which the android application sends post data to a php file which that then authenticates the user to see if the credentials are correct or not for the login. I have been trying to get this working all afternoon but it seems all my attempts of importing JFactory are failing.

I have the below code which bugs out at the first mention of JFactory. My attempt of importing it is not working either.

<?php
//needed to be commented out otherwise the android application cannot send data to the file
//defined('_JEXEC') or die;

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

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE.'/includes/helper.php';
require_once JPATH_BASE.'/includes/toolbar.php';
require JPATH_BASE.'/library/joomla/factory.php';

$email = $_POST['email'];
$password = $_POST['password'];
file_put_contents("Log.txt", "got data: ".$email." and ".$password);
$credentials->email = $email;
$credentials->password = $password;
$responce;

//error occurs here
$db     = JFactory::getDbo();
...
?>

I have looked at these other questions about importing JFactory but none of them are working for me:
- JFactory,JDatabase class not found in /var/www/joomla2.5/database.php
- Class 'JFactory' not found
- include Jfactory class in an external php file, Joomla

So my simplified question is how do I import JFactory or at least work around it in this situation? Anyone feeling smarter than me who would be so kind to answer the question :)

General Information:

  • running php 5.1.13
  • Joomla 2.5
  • Testing on wamp server (localhost)

2条回答
该账号已被封号
2楼-- · 2019-03-03 21:52

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.

查看更多
聊天终结者
3楼-- · 2019-03-03 21:56

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..

查看更多
登录 后发表回答