【问题标题】:In Joomla 3.4 JFactory::getApplication('site'); stopped working在 Joomla 3.4 JFactory::getApplication('site');停止工作
【发布时间】:2016-10-25 08:25:25
【问题描述】:

我使用了来自此参考的以下代码:https://stackoverflow.com/a/2288181/4819200

<?php
//http://domain.com/script/script.php?username=username&passwd=password

define( '_JEXEC', 1 );
define('JPATH_BASE', '../' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once('../configuration.php');
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );

/* Create the Application */
$mainframe =& JFactory::getApplication('site');
jimport('joomla.plugin.helper');

$credentials = array();
$credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
$credentials['password'] = JRequest::getVar('passwd', '', 'method', 'passwd');

//perform the login action
$error = $mainframe->login($credentials);
$user = JFactory::getUser();
//now you are logged in

$mainframe->logout();
//now you are logged out

现在,问题

它在 Joomla 2.3 版本中工作,在 3.4 版本中停止工作。 现在,它没有从 $mainframe =& JFactory::getApplication('site'); 行返回。

你可以建议我最新版本的代码。

注意:我不是 joomla 人。但是,我研究过一些 PHP 框架。

【问题讨论】:

    标签: web-services login joomla


    【解决方案1】:

    在 Joomla 3 中,您可以使用此代码从 Joomla 外部获取用户对象

    <?php
    //http://domain.com/script/script.php?username=username&passwd=password
    
    $dir = '/var/www/joomla'; // path to your joomla installation directory
    
    define( '_JEXEC', 1 );
    define( 'JPATH_BASE', $dir);
    define( 'DS', '/' );
    
    require_once ( JPATH_BASE .DS . 'configuration.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    
    require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
    //Then you can call the classes this way
    $mainframe = JFactory::getApplication('site');
    
    /* Create the Application */
    //$mainframe =& JFactory::getApplication('site');
    jimport('joomla.plugin.helper');
    
    $credentials = array();
    $credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
    $credentials['password'] = JRequest::getVar('passwd', '', 'method', 'passwd');
    
    //perform the login action
    $error = $mainframe->login($credentials);
    
    $user = JFactory::getUser();
    //now you are logged in
    
    $mainframe->logout();
    

    【讨论】:

    • 我已经在我的视线中测试了这段代码,它停止了 JFactory::getApplication('site'); 的执行。我做了进一步测试,它正在调用库/joomla/application/application.php 文件。它不在我们的新版本 (3.4) joomla 中。
    • 我正在使用 Joomla 3.4.4 版,它工作正常。那么让我检查一下 3.5。
    • 你能告诉我你是怎么发现它正在调用libraries/joomla/application/application.php文件的,以便我可以交叉检查吗?
    • 我已经备份了 2.4 版本,其中我发现了一个名为 application.php 的类,它是从 JFactory::getApplication 调用的。而在 3.5 版本中,没有名为 application.php 的类。我知道,我不应该调试核心文件。但我仍然想知道是什么导致了这个问题。
    • 在 Joomla 3.x 中,您应该使用JRequestgetVar(),因为它们已被弃用。请看JInput
    【解决方案2】:
    $code = NULL;
    $plugin_param->params = new JRegistry($plugin_param->params);
    $is_valid = $plugin->checkAnswer($code);
    

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2015-05-07
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多