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

标签 php laravel user-registration octobercms

我正在尝试学习 OctoberCMS,我对扩展插件的完整过程感到困惑。我根据截屏视频 ( https://vimeo.com/108040919 ) 扩展了用户插件。最终,我希望创建一个名为“类别”的新字段,用于存储用户类别。在新页面上,我有以下表格,我试图使用它来仅根据他们的电子邮件地址注册新用户。 “类别”根据他们注册的页面填充,密码应自动生成,以便用户在通过电子邮件激活链接确认其帐户后设置密码。我的插件叫做“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')
                ]
            ]);
        });
    }

}

我的 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;
    }

}

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

<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>

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

最佳答案

好吧,我只需要为我自己的网站做这样的事情。因此,我将尝试解释您的 2 个选项。
1:使用主题和页面 php 覆盖内容。

  1. 覆盖表单。为此,复制 register.htm 从 插件/rainlabs/用户/组件/帐户/register.htm 至 主题/站点主题/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);
        }
    } 
    ==
    

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
    
  4. 然后更新插件的 Plugin.php 文件以激活组件:

        public function registerComponents()
        {
            return [
               'Foo\Bar\Components\AccountExtend' => 'account',
            ];
        }
    
  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;
      }
    

关于php - OctoberCMS 如何使用插件扩展中的字段创建自定义用户注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681748/

相关文章:

php - Laravel 中的条件路由

Facebook 注册插件 : "invalid_id" when logging out

php - 有效地检查组中是否存在任何值以及它是哪一个?

php - echo mysql DATE(table_date) 未定义常量

php - 如何使用一种表单中的两个按钮执行不同的 JavaScript 操作?

laravel - 如何使用 docker 容器从主机运行 artisan 命令

authentication - 传递给 Illuminate\Auth\Guard::login() 的参数 1 必须实现接口(interface) Illuminate\Auth\UserInterface,null 给定打开:

python - Django 注册不起作用

django - 故障排除 "TemplateDoesNotExist at/accounts/login/"- Django 身份验证设置

PHP 未定义的常量错误没有意义