微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-通过添加配置文件扩展用户插件不会在OctoberCMS中渲染选项卡或新添加的字段

我已经按照Extending User plugin屏幕截图中的所有步骤进行操作,但是由于某些原因,我看不到“配置文件”选项卡和任何新添加的字段.由于我使用了第二种方法(一种简单的方法),因此我已经完成了这一工作:

>在Alomicuba名称空间下创建插件和模型等
>按照视频中的说明创建文件并进行所需的更改:

Plugin.PHP

<?PHP namespace Alomicuba\Profile;

use System\Classes\PluginBase;
use RainLab\User\Models\User as usermodel;
use RainLab\User\Controllers\Users as UsersController;

/**
 * Profile Plugin information File
 */
class Plugin extends PluginBase
{
    public $requires = ['RainLab.User'];

    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'Profile',
            'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User',
            'author'      => 'DTS',
            'icon'        => 'icon-users'
        ];
    }

    public function boot()
    {
        usermodel::extend(function($model){
            $model->hasOne['profile'] = ['Alomicuba\Profile\Models\Profile'];
        });

        UsersController::extendFormFields(function ($form, $model, $context){
            if ($model instanceof usermodel)
                return;

            $form->addTabFields([
                'pinCode' => [
                    'label' => 'PIN',
                    'tab' => 'Profile'
                ],
                'phone2' => [
                    'label' => 'Teléfono (2)',
                    'tab' => 'Profile'
                ],
                'phone3' => [
                    'label' => 'Teléfono (3)',
                    'tab' => 'Profile'
                ],
                'phone4' => [
                    'label' => 'Teléfono (4)',
                    'tab' => 'Profile'
                ]
            ]);
        });
    }
}

add_profiles_fields_to_user_table.PHP

<?PHP namespace Alomicuba\Profile\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class AddProfilesFieldsToUserTable extends Migration
{

    public function up()
    {
        Schema::table('users', function($table)
        {
            $table->integer('pinCode')->unsigned();
            $table->dateTime('pinCodeDateTime');
            $table->integer('phone2')->unsigned()->nullable();
            $table->integer('phone3')->unsigned()->nullable();
            $table->integer('phone4')->unsigned()->nullable();
        });
    }

    public function down()
    {
        $table->dropDown([
            'pinCode',
            'pinCodeDateTime',
            'phone2',
            'phone3',
            'phone4'
        ]);
    }
}

version.yaml
1.0.1: First version of Profile
1.0.2:
    - Created profiles table
    - create_profiles_table.PHP
    - add_profiles_fields_to_user_table.PHP

Profile.PHP (Model)
<?PHP namespace Alomicuba\Profile\Models;

use Model;

/**
 * Profile Model
 */
class Profile extends Model
{
    /**
     * @var string The database table used by the model.
     */
    public $table = 'alomicuba_profile_profiles';

    /**
     * @var array Relations
     */
    public $belongsTo = [
        'user' => ['RainLab\User\Models\User']
    ];


    // This method is not need anymore since I'll use the second approach
    public static function getFromUser($user)
    {
        if ($user->profile)
            return $user->profile;

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

        $user->profile = $profile;

        return $profile;
    }
}

但是,当我编辑现有用户时,我没有看到“个人资料”标签,也没有看到任何新添加的字段.见下图:

有什么建议吗?我错过了什么?

另外我对插件扩展有一些疑问:

>如何将必填字段添加注册表?
>如何在帐户表单上显示每个新添加的字段?

解决方法:

我已经在我的机器上测试过您的代码,您需要编写

在plugin.PHP中以$require代替$requires

检查文件

http://octobercms.com/docs/plugin/registration#dependency-definitions

并且当为UserController调用extendFormFields()方法时,您需要指定您只想扩展usermodel的字段,而不扩展其他字段

if (!$model instanceof usermodel)
    return;

所以plugin.PHP代码看起来像这样

<?PHP namespace Alomicuba\Profile;

use System\Classes\PluginBase;
use RainLab\User\Models\User as usermodel;
use RainLab\User\Controllers\Users as UsersController;

/**
 * Profile Plugin information File
 */
class Plugin extends PluginBase
{
    public $require = ['RainLab.User'];

    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'Profile',
            'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User',
            'author'      => 'DTS',
            'icon'        => 'icon-users'
        ];
    }

    public function boot()
    {
        usermodel::extend(function($model){
            $model->hasOne['profile'] = ['Alomicuba\Profile\Models\Profile'];

        });

        UsersController::extendFormFields(function ($form, $model, $context){
            if (!$model instanceof usermodel)
                return;

            $form->addTabFields([
                'pinCode' => [
                    'label' => 'PIN',
                    'tab' => 'Profile'
                ],
                'phone2' => [
                    'label' => 'Teléfono (2)',
                    'tab' => 'Profile'
                ],
                'phone3' => [
                    'label' => 'Teléfono (3)',
                    'tab' => 'Profile'
                ],
                'phone4' => [
                    'label' => 'Teléfono (4)',
                    'tab' => 'Profile'
                ]
            ]);
        });
    }
}

和在add_profiles_fields_to_user_table.PHP

用于删除列,编写以下代码

Schema::table('users', function($table)
{
        $table->dropDown([
            'pinCode',
            'pinCodeDateTime',
            'phone2',
            'phone3',
            'phone4'
        ]);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐