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

标签 php laravel laravel-4 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;
        }
    }
    

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

enter image description here

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

还有一些关于插件扩展的问题:

  1. 如何在注册表单中添加必填字段?
  2. 如何在帐户表单上显示每个新添加的字段?

最佳答案

我已经在我的机器上测试了你需要编写的代码

$require 而不是 plugin.php 中的 $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'
                ]
            ]);
        });
    }
}

enter image description here

并在 add_profiles_fields_to_user_table.php 中

要删除列,请编写以下代码

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

关于php - 通过添加配置文件扩展用户插件不会在 OctoberCMS 中呈现选项卡或新添加的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26717505/

相关文章:

php - 当出现一些依赖错误时,如何停止在 Laravel 中执行 Create 操作

php - 多个mysql查询的问题

php - 无法在 Advanced::with() 语句中选择列

laravel - Eloquent ORM - 检索与给定模型无关的模型(多对多关系)

php - 将 Laravel 用于服务 worker ?

php - 为 Laravel 应用程序中的所有表单设置全局选项

php - 如何检查包含加密电子邮件地址的表中是否存在电子邮件地址

php - 使用个人访问 token 在 Postman 中未经身份验证的 Laravel 5.3 Passport API

php - Laravel map () : How to alter objects and arrays?

heroku - 如何在 Heroku 中使用 Laravel4 的 php artisan migrate 命令?