当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《OctoberCMS如何使用插件扩展中的字段创建自定义用户注册表单》经验,为你挑选了1个好方法。

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

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

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

}

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

 ['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;
    }

}

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

;

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



1> 小智..:

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

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

    在您的帐户登录/注册页面上,将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);
        }
    } 
    ==
    

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

    php artisan create:component Foo.Bar AccountExtend来创建你的组件

    现在进入组件并改变一些事情.首先,您需要将所有文件从plugins/rainlabs/user/components/account /复制到plugins/Foo/Bar/components/accountextend /

    现在您需要更新组件AccountExtend.php.首先,进行一些更改以使其正常工作(我没有包含所有代码,只需要更改的内容):

    use RainLab\User\Components\Account as UserAccount;
    
    class AccountExtend extends UserAccount
    

    然后更新插件的Plugin.php文件,以激活组件:

        public function registerComponents()
        {
            return [
               'Foo\Bar\Components\AccountExtend' => 'account',
            ];
        }
    

    现在,您可以将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;
      }
    

推荐阅读
黄晓敏3023
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有