Laravel - 尝试创建帐户,登录并发送激活电子邮件正在抛出“尝试获取非对象的属性”
Posted
技术标签:
【中文标题】Laravel - 尝试创建帐户,登录并发送激活电子邮件正在抛出“尝试获取非对象的属性”【英文标题】:Laravel - Trying to create acocunt, signin and send activation email is throwing"Trying to get property of non-object" 【发布时间】:2014-10-01 08:02:39 【问题描述】:最初,该过程效果很好,但用户必须先激活他们的帐户才能登录
因此,我正在调整我的注册流程,以便用户可以在他们注册后立即访问仪表板,然后发送一封激活电子邮件(因此应用的其他部分将保持非活动状态,直到他们确认为止)。
这是最新的代码
public function postSignup()
// Create account form validation
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:50|min:3|unique:users',
'password' => 'required|min:6',
'password_again' => 'required|same:password'
)
);
// test if validation is working
if ($validator->fails()) // If validation fails
return Redirect::route('signup') // redirect to new account page
->withErrors($validator) // Show the errors
->withInput(); // Show original form input
else
// get the inputs and put them in to varibles
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('password');
// Generate an activation code
$code = str_random(60);
// Create new user in database
$user = User::create(array(
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'active' => 0
));
// log the user into the app
$user = Auth::attempt(array(
'email' => $email,
'password' => $password
));
if($user)
// Send activation email
Mail::send('app.register.emails.activate', array('link' => URL::route('account-activate', $code), 'username' => $username), function($message) use ($user)
$message->to($user->email, $user->username)->subject('Activate your account');
);
// When completed send the user to dashboard
return Redirect::route('dashboard')
->with('global', 'Your account has been created, we have sent you an email to confirm'); // send a global confirmation message
好像是下面的代码
$user = Auth::attempt(array(
'email' => $email,
'password' => $password
));
与
发生冲突Mail::send('app.register.emails.activate', array('link' => URL::route('account-activate', $code), 'username' => $username), function($message) use ($user)
$message->to($user->email, $user->username)->subject('Activate your account');
);
我可以删除 Auth::attempt 并发送邮件,但我必须重新登录,或者我删除 Mail::send 并注册并登录(不发送确认电子邮件),但如果我有我得到和错误异常:
“试图获取非对象的属性”
对 php/Laravel 相当陌生,因此我们将不胜感激。
谢谢,杰克
【问题讨论】:
【参考方案1】:当您运行User::create()
时,您已经创建并填充了$user
变量
然后,当您运行 Auth::attempt()
时,您将用 true/false 覆盖 $user
只需将您的 Auth::attempt
代码更改为此,您的其余代码将正常工作(即不要再次重新分配 $user
变量);
Auth::attempt(array(
'email' => $email,
'password' => $password
));
【讨论】:
谢谢 - 我还必须更改流程的顺序,因为仅从 Auth 中删除 $user 本身并不起作用。谢谢你的解释。以上是关于Laravel - 尝试创建帐户,登录并发送激活电子邮件正在抛出“尝试获取非对象的属性”的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 5.4 - 在已经登录的情况下创建另一个用户帐户