每个人。我目前在修改 Laravel 5.2 中的 Auth 注册系统时遇到问题。我想要做的是为普通用户和管理员提供两个独立的日志系统。我在数据库中为名为 admin 的用户添加了一个新字段,并将默认值设置为 0 或 false。使用 Laravel 的 auth 系统使用的注册页面,我添加了一个隐藏的输入,并将值设置为 1。这个页面显然是用于登录管理员,以便他们可以进入管理区域。
当我提交表单时,管理字段被简单地设置为默认值,这不是我想要的站点的这一部分。一旦用户单击此页面上的提交,我希望他的信息以管理员值 1 发送到数据库。有没有办法做到这一点?
到目前为止,这是我为自定义身份验证注册系统所做的工作。
我删除了 name 字段并添加了 firstname 和 lastname 字段,以便用户可以在单独的文本输入中输入这两个值。我更新了 User 模型以将这些字段包含在 $fillable 数组中,并更新了 AuthControllers 验证方法,以便可以验证这些字段。
这是每个的代码
这是 User.php 模型文件
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname', 'lastname', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'admin',
];
}
这里是 AuthContoller 文件
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'admin' => 'required',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'admin' => $data['admin'],
]);
}
}
如您所见,我还在 User.php 模型文件中添加了 admin 字段,希望它会自动填充。
如果有人能告诉我如何做到这一点,那就太好了。但是正如你所看到的,这对于一个实际的网站来说并不是一个真正的实用解决方案,但它更多的是关于学习 Laravel 的来龙去脉并学习如何定制它。当我开始写这个问题时,在我看来,我可以轻松地为任何用户使用注册,然后允许上级管理员将 admin 字段的值设置为 true,以便用户可以冒险进入应用程序的管理部分。当然,像这样的系统,没有必要有两个单独的登录屏幕,但就像我说的,我只是在学习。
我还计划添加角色,类似于不同级别的管理员的文字印刷机,这将控制诸如创建、编辑和发布诸如帖子之类的内容之类的事情。但现在,我只是按原样开发这个系统。
感谢您对此主题的任何帮助和评论。