laravel OctoberCMS 如何使用插件扩展中的字段创建自定义用户注册表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34681748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OctoberCMS How To Create Custom User Registration Form With Fields From Plugin Extension
提问by user2694306
I'm trying to learn OctoberCMS and I am confused about the complete process of extending plugins. I've extended the user plugin according to the screencast (https://vimeo.com/108040919). Ultimately, I'm looking to create a new field called "category" which wold store a users category. On a new page, I have the following form which I am trying to use to register a new user based solely on their e-mail address. The "category" is populated based on the page that they registered from and the password should be automatically generated so that the user will set it upon confirming their account from an e-mail activation link. My plugin is called "Profile".
我正在尝试学习 OctoberCMS,但我对扩展插件的完整过程感到困惑。我已经根据截屏视频 ( https://vimeo.com/108040919)扩展了用户插件。最终,我希望创建一个名为“类别”的新字段,用于存储用户类别。在一个新页面上,我有以下表格,我试图使用它来仅根据他们的电子邮件地址注册新用户。“类别”是根据他们注册的页面填充的,密码应自动生成,以便用户在通过电子邮件激活链接确认其帐户后进行设置。我的插件被称为“配置文件”。
My plugin.php file looks like:
我的 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')
]
]);
});
}
}
My profile.php file looks like:
我的 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;
}
}
I am trying to create a user registration form that looks like the following:
我正在尝试创建一个如下所示的用户注册表单:
<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>
What I am confused about is how to make an "onSignup" component that will basically extend the functionality of the user plugins "onRegister" component and then automatically generate a password and also save the "category" field. Can anyone provide an example or link to a page that shows an example of this? Thanks.
我感到困惑的是如何制作一个“onSignup”组件,它基本上会扩展用户插件“onRegister”组件的功能,然后自动生成密码并保存“类别”字段。任何人都可以提供一个示例或链接到显示此示例的页面吗?谢谢。
回答by Fooldj
Okay, I just had to do something like this for my own site. So I'll try and explain the 2 options you have.
1: Override stuff using the theme and page php.
好的,我只需要为我自己的网站做这样的事情。因此,我将尝试解释您拥有的 2 个选项。
1:使用主题和页面 php 覆盖东西。
Override the form. To do this, copy register.htm from plugins/rainlabs/user/components/account/register.htm to themes/site-theme/partials/account/register.htm. You can now update the form to have any fields you want.
On your account login/register page, put php in the php section to override the default onRegister() function:
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); } } ==
覆盖表单。为此,请将 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: Create a component to do it all. This is the way I ended up going
2:创建一个组件来完成这一切。这就是我最终去的方式
- php artisan create:component Foo.Bar AccountExtend to create your component
- Now go into the component and change some things around. First you'll need to copy all the files from plugins/rainlabs/user/components/account/ to plugins/Foo/Bar/components/accountextend/
Now you need to update the component AccountExtend.php. First, some changes to make it work right (I haven't included all code, just stuff that needs changes):
use RainLab\User\Components\Account as UserAccount; class AccountExtend extends UserAccount
And then update the Plugin.php file for your plugin, to activate the component:
public function registerComponents() { return [ 'Foo\Bar\Components\AccountExtend' => 'account', ]; }
Now you can add the onRegister() function to your 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 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; }
回答by Eric Kelly
It's definitely confusing. Take a look at the User Plus plugin as a guide and the locations plugin (required) to add country/state dropdown.
这绝对令人困惑。看看 User Plus 插件作为指南和位置插件(必需)以添加国家/州下拉列表。
You'll have to add a new table or add a field to user's table using "updates/migrations" to add something like "category_id" for the user. it will save it for you.
Make sure this is set fillable as user creation will fill the model with the form data otherwise it won't save. this is on boot, the user's plus plugin has this so check it out.
Make a new table for you're categories, and make a component part that can fetch the list. see the locations plugin for this... the part that fetches the data/seeds the list is in the component.
You'll need to make a new component partial or use it right in the cms partial specifying the new fields.
您必须添加一个新表或使用“更新/迁移”向用户表添加一个字段,以便为用户添加诸如“category_id”之类的内容。它会为你保存它。
确保将其设置为可填充,因为用户创建将使用表单数据填充模型,否则将无法保存。这是在启动时,用户的 plus 插件有这个,所以检查一下。
为您的类别制作一个新表,并制作一个可以获取列表的组件。请参阅位置插件...获取数据/种子列表的部分在组件中。
您需要制作一个新组件部分或在指定新字段的 cms 部分中正确使用它。
Far as making the password random, not 100% the best way but you can use onInit for your component to just seed one in the form post data. the password field has to be added to the post data since the user's plugin passes the post fields to the model (which is why you must set category_id as fillable or it gets blocked for security reasons).
至于使密码随机,不是 100% 最好的方法,但您可以使用 onInit 为您的组件只在表单发布数据中播种一个。密码字段必须添加到帖子数据中,因为用户的插件将帖子字段传递给模型(这就是为什么您必须将 category_id 设置为可填写或出于安全原因被阻止)。
I am messing with this stuff myself and the users plus with the location plugin helped a lot.
我自己在搞这些东西,用户加上位置插件帮助了很多。
Sorry i couldn't be more detailed.
对不起,我不能更详细了。