Laravel 4使用Zizaco / confide以编程方式创建用户

时间:2014-03-28 13:31:32

标签: php laravel-4

我正在尝试使用Zizaco / confide库创建用户,用户数据来自Facebook,而不是来自表单。

我试试这个:

$user_profile = Facebook::api('/me','GET');

$new_user = new User;

$new_user->username = $user_profile['name'];
$new_user->password = Hash::make('secret');
$new_user->email = $user_profile['email'];
$new_user->confirmation_code = '456456';
$new_user->confirmed = true;

$new_user->save();

但它不会保存用户。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

我发现问题,confide库设置了一些默认规则来创建用户,你需要通过这个规则来保存用户:

public static $rules = array(
    'username' => 'required|alpha_dash|unique:users',
    'email' => 'required|email|unique:users',
    'password' => 'required|between:4,11|confirmed',
    'password_confirmation' => 'between:4,11',
);

使用这个例子它可以工作:

$user = new User();

$user->username = 'asdasd';
$user->email = 'angorusadf@gmail.com';
$user->password = '12312312';
$user->password_confirmation = '12312312';

$user->save();

Mabe方法应该在你没有通过规则时给你一些信息。

答案 1 :(得分:1)

也许这是一个很晚的答案,但是我发布这个以供将来参考,因为我自己在寻找同一个问题的答案,How to create a user programatically in zizaco/confide?或更一般地说,{{1} }

嗯,答案非常简单,多亏了Zizaco在4. *分支中实现的新架构,现在可以注册一个新的用户验证器类了解更多信息,请参见the package's readme但是,在这种情况下,您只需要扩展当前的用户验证器并覆盖how to bypass zizaco/confide's requirement for setting a password when saving a user for the first time?,使其接受新用户的空密码。

以下是一个示例实现:

validatePassword()      

routes.php

// we need to register our validator so that it gets used instead of the default one // Register custom Validator for ConfideUsers App::bind('confide.user_validator', 'MyUserValidator'); 中(基本上是该类函数的副本,只需添加一个检查,看看这是否是旧用户(如果用户有ID,那么这是一个更新operation)如果这是一个新用户,该方法总是返回true!

app/models/MyUserValidator.php
相关问题