2

I've searched and found a way to do this with JUser but when I try the script it says an include file can't be found and it doesn't exist on the server. I don't know if this is different for Joomla 3.0 so I was asking for help. Here's the script I tried:

<?php
   define( '_JEXEC', 1 );
   define('JPATH_BASE', dirname(__FILE__) );
   define( 'DS', DIRECTORY_SEPARATOR );
   /* 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_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
   require_once JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'application'.DS.'component'.DS.'helper.php';
   $model = new UsersModelRegistration();
   jimport('joomla.mail.helper');
   jimport('joomla.user.helper');

   $username = 'jimporttest';
   $name = 'J Port2';
   $email = 'test @ mail.com';
   $password = 'test';
   $data = array( 'username' => $username,
             'name' => $name,
             'email1' => $email,
             'password1' => $password, // First password field
             'password2' => $password, // Confirm password field
             'block' => 0 );
   echo $model->register($data);
?>
4

4 回答 4

4

您必须在 require_once 子句中使用 JPATH_BASE(您使用的是未定义的 JPATH_BASE_ROOT)。同样在 JOOMLA 3.0 中,helper.php 不在那个位置。

试试这个代码:

<?php
         define( '_JEXEC', 1 );
         define('JPATH_BASE', dirname(__FILE__) );
         define( 'DS', DIRECTORY_SEPARATOR );
         /* 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';
        //not necessary
        //require_once(JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'application'.DS.'component'.DS.'helper.php';
         $model = new UsersModelRegistration();
         jimport('joomla.mail.helper');
         jimport('joomla.user.helper');

         $username = 'jimporttest';
         $name = 'J Port2';
         $email = 'test @ mail.com';
         $password = 'test';
         $data = array( 'username' => $username,
         'name' => $name,
         'email1' => $email,
         'password1' => $password, // First password field
         'password2' => $password, // Confirm password field
         'block' => 0 );
          echo $model->register($data);
  ?>

我已经在我的 joomla 3.0 安装中尝试过了,它可以工作。

安德烈亚

于 2013-10-29T11:02:15.670 回答
0

除非您的文件位于安装 Joomla 系统的根文件夹中,否则肯定会出现此错误。

dirname(__FILE__)

返回工作文件路径。因此,您可能需要相应地修改路径。利用

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
  echo JPATH_BASE;

确保与 JPATH_BASE 相同路径中的“包含”文件夹重新启动。例如,如果您的文件位于“www/example/test”文件夹中,则使用

define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));

获得正确的基本路径。

我还在您的代码中发现了一些错误。将此工作代码用于测试目的。

            <?php
                define( '_JEXEC', 1 );
                define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));
                define( 'DS', DIRECTORY_SEPARATOR );

                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_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
                $model = new UsersModelRegistration();
                jimport('joomla.mail.helper');
                jimport('joomla.user.helper');


                $username = 'ben';
                $name = 'ben';
                $email = 'test@mail.com';
                $password = 'test';
                $data = array( 'username' => $username,
                         'name' => $name,
                         'email1' => $email,
                         'password1' => $password, // First password field
                         'password2' => $password, // Confirm password field
                         'block' => 0 );

                $return = $model->register($data);
            ?>

'../../' 的一部分

define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));

取决于我之前解释的根文件夹的偏差。

于 2015-01-18T01:00:32.223 回答
0

对于 >=Joomla 3,我们需要使用以下代码。从“JModel”更改为“JModelLegacy”。点击这里查看详细信息。

$data['name'] = $fname . ' ' . $lname;
$data['username'] = $email;
$data['email1'] = $email;
$data['email2'] = $email;
$pwd = substr(md5($email . $fname . time()), 0, 6);
$data['password1'] = $pwd;
$data['password2'] = $pwd;

JFactory::getLanguage()->load('com_users');
JModelLegacy::addIncludePath(JPATH_ROOT . '/components/com_users/models');

$model = JModelLegacy::getInstance('Registration', 'UsersModel');
$return = $model->register($data);
于 2014-10-27T14:30:01.170 回答
0

这是您可以使用的代码。

$data['name'] = $fname . ' ' . $lname;
$data['username'] = $email;
$data['email1'] = $email;
$data['email2'] = $email;
$pwd = substr(md5($email . $fname . time()), 0, 6);
$data['password1'] = $pwd;
$data['password2'] = $pwd;

JFactory::getLanguage()->load('com_users');
JModel::addIncludePath(JPATH_ROOT . '/components/com_users/models');

$model = JModel::getInstance('Registration', 'UsersModel');
$return = $model->register($data);
于 2013-03-25T15:20:18.537 回答