Joomla PHP API 获取日志记录的错误消息

Joomla PHP API get error messages for logging

我正在使用我自己的脚本访问 Joomla PHP API,并且我编写了这个简单的注册函数,它是从移动应用程序调用的。

/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();

require_once(JPATH_BASE.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php');

$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
jimport('joomla.user.helper');
$language = JFactory::getLanguage();
$language->load('com_users', JPATH_SITE);

$username = JRequest::getVar('username', '', 'method', 'username');
$name = JRequest::getVar('name', '', 'method', 'name'); 
$email = JRequest::getVar('email', '', 'method', 'email'); 
$password = JRequest::getVar('password', '', 'method', 'password');

$data = array( 
'username' => $username,
'name' => $name,
'email1' => $email,
'password1' => $password, // First password field
'password2' => $password, // Confirm password field
'block' => 0);


if($model->register($data) == "useractivate") {
    print "Registered";
    //log successfull registration data maybe

}
else {
    print "Failed to register";
    //DEFINITELY LOG WHY IT FAILED
}

有什么方法可以在注册失败时得到错误,在其他部分?

$model 是否包含与此相关的任何数据?

如果我的判断正确,那么您正在寻找 getErrors 函数。

// Get the validation messages.
$errors = $model->getErrors();

// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
    if ($errors[$i] instanceof Exception)
    {
        $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
    }
    else
    {
        $app->enqueueMessage($errors[$i], 'warning');
    }
}