我正在尝试为我的2.5 Joomla网站的Android应用程序创建一个登录系统。我试图通过制作一个Joomla插件来实现这一点,Android应用程序将发布数据发送到php文件,然后验证用户是否正确登录凭据。我一直试图让这个工作整个下午,但似乎我所有导入JFactory的尝试都失败了。
我在第一次提到JFactory时遇到了以下代码。我尝试导入它也不起作用。
<?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();
...
?>
我已经查看了有关导入JFactory的其他问题,但没有一个对我有用:
- 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
所以我的简化问题是如何导入JFactory或至少在这种情况下解决它?任何比我更聪明的人都会如此友善地回答这个问题:)
一般信息:
答案 0 :(得分:3)
我建议采用正式的Joomla方式并启动应用程序。我做的很好,是推荐的Joomla方式(我没有测试下面的代码,但它应该给你一个起点,因为它是我的代码中来自多个来源的复制粘贴)。
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();
有些东西适用于我。可在此处找到更多平台示例https://github.com/joomla/joomla-platform-examples/tree/master/web。
答案 1 :(得分:0)
试试这个,
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();
它会工作,你错过了JFactory::getApplication('site');
希望有所帮助......