2013-09-28 59 views
0

我想爲我的2.5 Joomla網站的Android應用程序製作一個登錄系統。我正在嘗試通過製作一個Joomla插件來實現這一點,即Android應用程序將發佈數據發送到一個php文件,然後通過該文件對用戶進行身份驗證,以查看登錄憑據是否正確。我一直試圖在下午完成這項工作,但似乎我所有的導入JFactory的嘗試都失敗了。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或至少在這種情況下解決它?任何人都覺得比我聰明會是誰這麼好心來回答這個問題:)

一般資料:

  • 運行PHP 5.1.13
  • 的Joomla 2.5
  • 測試WAMP的服務器上(本地主機)
+0

我建議升級到PHP 5.3 – Laoneo

回答

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

+0

工程就像一個魅力!由於文件的位置,只需修改'JPATH_BASE'。隊友的歡呼聲! – Nick

+0

我不明白你爲什麼在第三行代碼中得到實例「MyJoomlaServer」。這是在API還是默認? – Nick

+1

是的,這是API。 – Laoneo

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

希望它可以幫助..