簡體   English   中英

JFactory無法導入

[英]JFactory failing to import

我正在嘗試為適用於我的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的其他問題,但沒有一個對我有用:
- 在/var/www/joomla2.5/database.php中找不到JFactory,JDatabase類
- 找不到“ JFactory”類
- 在外部php文件Joomla中包含Jfactory類

因此,我的簡化問題是在這種情況下如何導入JFactory或至少解決該問題? 任何人都比我聰明,願意回答這個問題:)

一般信息:

  • 運行PHP 5.1.13
  • Joomla 2.5
  • 在Wamp服務器(本地主機)上測試

我建議采用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

嘗試這個,

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');

希望能幫助到你..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM