自定义用户提供程序
创建自己的 UserModel
如果您想自定义用户属性,则需要创建自己的 用户提供程序 类。唯一的要求是您的新类必须扩展提供的 CodeIgniter\Shield\Models\UserModel
。
Shield 有一个 CLI 命令,可以通过在终端中运行以下命令来快速创建一个自定义 UserModel
类
php spark shield:model UserModel
类名是可选的。如果没有提供,则生成的类名将为 UserModel
。
配置以使用您的 UserModel
创建类后,将您的模型类名设置为 app/Config/Auth.php 中的 $userProvider
属性
public string $userProvider = \App\Models\UserModel::class;
自定义您的 UserModel
根据您的喜好自定义您的模型。
如果您添加属性,请不要忘记将属性添加到 $allowedFields
属性。
<?php
declare(strict_types=1);
namespace App\Models;
use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;
class UserModel extends ShieldUserModel
{
protected function initialize(): void
{
parent::initialize();
$this->allowedFields = [
...$this->allowedFields,
'first_name', // Added
'last_name', // Added
];
}
}