OctoberCMS如何使用插件扩展中的字段创建自定义用户注册表单

use*_*306 15 php user-registration laravel octobercms

我正在尝试学习OctoberCMS,我对扩展插件的完整过程感到困惑.我根据截屏视频(https://vimeo.com/108040919)扩展了用户插件.最终,我正在寻找创建一个名为"category"的新领域,该领域将存储用户类别.在新页面上,我有以下表单,我试图用它来仅根据他们的电子邮件地址注册新用户.根据他们注册的页面填充"类别",并且应自动生成密码,以便用户在通过电子邮件激活链接确认其帐户时进行设置.我的插件名为"Profile".

我的plugin.php文件如下所示:

<?php namespace Sser\Profile;

use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use RainLab\User\Controllers\Users as UsersController;
use Sser\Profile\Models\Profile as ProfileModel;
/**
 * Profile Plugin Information File
 */
class Plugin extends PluginBase
{

    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'Profile',
            'description' => 'Handles user demographic information',
            'author'      => '',
            'icon'        => 'icon-leaf'
        ];
    }

    public function boot()
    {
        UserModel::extend(function($model){
            $model->hasOne['profile'] = ['Sser\Profile\Models\Profile'];
        });
        // $user->profile->zip

        UserModel::deleting(function($user) {
            $user->profile->delete();
        });

        UsersController::extendFormFields(function($form,$model,$context){
            if(!$model instanceof UserModel)
            {
                return;
            }
            if(!$model->exists)
            {
                return;
            }
            //Ensures that a profile model always exists...
            ProfileModel::getFromUser($model);

            $form->addTabFields([
                'profile[age]'=>[
                    'label'=>'Age',
                    'tab'=>'Profile',
                    'type'=>'number'
                ],
                'profile[gender]'=>[
                    'label'=>'Gender',
                    'tab'=>'Profile',
                    'type'=> 'dropdown',
                    'options'=>array('male'=>'Male',
                                     'female'=>'Female')

                ],
                'profile[category]'=>[
                    'label'=>'Category',
                    'tab'=>'Profile',
                    'type'=> 'dropdown',
                    'options'=>array('sink'=>'SINK',
                                     'dink'=>'DINK')
                ],
                'profile[vag]'=>[
                    'label'=>'VAG',
                    'tab'=>'Profile',
                    'type'=> 'dropdown',
                    'options'=>array('v'=>'V',
                                     'a'=>'A',
                                     'g'=>'G')
                ]
            ]);
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

我的profile.php文件如下所示:

<?php namespace Sser\Profile\Models;

use Model;
use \October\Rain\Database\Traits\Validation;

/**
 * Profile Model
 */
class Profile extends Model
{
    public $rules = [
        'category' => ['required', 'min:0']
    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'sser_profile_profiles';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */
    public $hasOne = [];
    public $hasMany = [];
    public $belongsTo = [
        'user'=> ['RainLab\User\Models\User']
    ];
    public $belongsToMany = [];
    public $morphTo = [];
    public $morphOne = [];
    public $morphMany = [];
    public $attachOne = [];
    public $attachMany = [];

    public static function getFromUser($user)
    {
        if($user->profile)
        {
            return $user->profile;
        }

        $profile = new static;
        $profile->user = $user;
        $profile->save();

        $user->profile = $profile;

        return $profile;
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个如下所示的用户注册表单:

<form class="flexiContactForm col s12" role="form" data-request="{{ __SELF__ }}::onSignup" data-request-update="'{{ __SELF__ }}::confirm': '.confirm-container'">;
    <button id="signup_button" class="waves-effect waves-light btn" style="float:right;" type="submit">Sign Up</button>
    <div style="overflow: hidden; padding-right:0em;">
    <input id="signup_email" type="email" class="validate" name="email">            
    <label id="signup_email_label" for="signup_email" data-error="" data-success="">Email Address</label>
    <input type="hidden" name="category" value="{{ data.category }}"/>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

令我困惑的是如何创建一个"onSignup"组件,它基本上扩展了用户插件"onRegister"组件的功能,然后自动生成密码并保存"category"字段.任何人都可以提供示例或链接到显示此示例的页面吗?谢谢.

小智 5

好的,我只需要为自己的网站做这样的事情.所以我会尝试解释你拥有的两个选项.
1:使用主题和页面php覆盖内容.

  1. 覆盖表单.为此,请将register.htm从plugins/rainlabs/user/components/account/register.htm复制到themes/site-theme/partials/account/register.htm.您现在可以更新表单以包含所需的任何字段.

  2. 在您的帐户登录/注册页面上,将php放入php部分以覆盖默认的onRegister()函数:

    title = "Account"
    url = "/account/:code?"
    layout = "default"
    description = "The account page"
    is_hidden = 0
    
    [account]
    redirect = "home"
    paramCode = "code"
    
    [session]
    security = "all"
    ==
    function onRegister()
    {
        try {
            //Do Your Stuff (generate password, category)
            //Inject the variables
            return $this->account->onSignin();
        }
        catch (Exception $ex) {
            Log::error($ex);
        }
    } 
    ==
    
    Run Code Online (Sandbox Code Playgroud)

2:创建一个组件来完成所有操作.这就是我结束的方式

  1. php artisan create:component Foo.Bar AccountExtend来创建你的组件
  2. 现在进入组件并改变一些事情.首先,您需要将所有文件从plugins/rainlabs/user/components/account /复制到plugins/Foo/Bar/components/accountextend /
  3. 现在您需要更新组件AccountExtend.php.首先,进行一些更改以使其正常工作(我没有包含所有代码,只需要更改的内容):

    use RainLab\User\Components\Account as UserAccount;
    
    class AccountExtend extends UserAccount
    
    Run Code Online (Sandbox Code Playgroud)
  4. 然后更新插件的Plugin.php文件,以激活组件:

        public function registerComponents()
        {
            return [
               'Foo\Bar\Components\AccountExtend' => 'account',
            ];
        }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在,您可以将onRegister()函数添加到AccountExtend.php:

        public function onRegister() {
            //Do anything you need to do
    
            $redirect = parent::onRegister();
    
            $user = $this->user(); // This is the user that was just created, here for example, dont need to assign it really
            // Now you can do stuff with any of the variables that were generated (such as user above)
    
           // Return the redirect so we redirect like normal
           return $redirect;
      }
    
    Run Code Online (Sandbox Code Playgroud)


小智 2

这绝对令人困惑。查看 User Plus 插件作为指南,以及位置插件(必需)来添加国家/州下拉列表。

  1. 您必须添加一个新表或使用“更新/迁移”向用户表添加一个字段,以便为用户添加“category_id”之类的内容。它会为你保存它。

  2. 确保将其设置为可填充,因为用户创建将使用表单数据填充模型,否则它将无法保存。这是启动时的,用户的 plus 插件有这个,所以检查一下。

  3. 为您的类别创建一个新表,并创建一个可以获取列表的组件。请参阅位置插件...获取列表中的数据/种子的部分位于组件中。

  4. 您需要创建一个新的组件部分或在指定新字段的 cms 部分中直接使用它。

至于使密码随机,不是 100% 最好的方法,但您可以使用 onInit 为您的组件在表单发布数据中播种一个密码。密码字段必须添加到帖子数据中,因为用户的插件将帖子字段传递给模型(这就是为什么您必须将 Category_id 设置为可填写,否则出于安全原因它会被阻止)。

我自己正在搞乱这些东西,用户加上位置插件帮助了很多。

抱歉我无法说得更详细。