Php Laravel:如何通过登录注册用户?
我正在使用身份验证控制器的标准登录/注册方法。目标是在用户登录时注册一个新用户(如果没有这样的用户),或者在有用户时只进行身份验证。在我看来,这应该只是重新分配几个方法。首先,我改变了Php Laravel:如何通过登录注册用户?,php,laravel,authentication,login,registration,Php,Laravel,Authentication,Login,Registration,我正在使用身份验证控制器的标准登录/注册方法。目标是在用户登录时注册一个新用户(如果没有这样的用户),或者在有用户时只进行身份验证。在我看来,这应该只是重新分配几个方法。首先,我改变了 AuthenticatesUsers.php public function login(Request $request) { $this->validateLogin($request); // If the class is using the Th
AuthenticatesUsers.php
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
//return $this->sendFailedLoginResponse($request);
}
With commenting the last line it will not say that there is no such user, and I believe right there I should put register method, but I can't find the right way to include it. I suggest that I should use `RegisterUsers.php`
AuthenticatesUsers.php是执行登录逻辑的控制器。我们正在查看公共功能登录
AuthenticatesUsers.php
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
trait AuthenticatesUsers
{
use RedirectsUsers, ThrottlesLogins, RegistersUsers;
public function showLoginForm()
{
return view('auth.login');
}
public function login(Request $request)
{
$this->validateLogin($request);
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
$this->incrementLoginAttempts($request);
//return $this->sendFailedLoginResponse($request);
$this->register($request);
}
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
protected function validator(array $data)
{
return Validator::make($data, [
//'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
//'password' => 'required|string|min:6|confirmed',
]);
}
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
protected function authenticated(Request $request, $user)
{
//
}
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
]);
}
public function username()
{
return 'email';
}
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
protected function guard()
{
return Auth::guard();
}
}
AuthenticatesUsers.php
只需将RegisterUsers.php
中的register
方法覆盖到LoginController
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
还可以从注册表控制器中添加验证程序
受保护的功能,并根据您的字段进行编辑
还要记住编辑用户
模型可填充
数组和创建用户
表的相对迁移文件。将Nullable()
设置为在登录期间不打算输入的字段 只需将RegisterUsers.php
中的register
方法覆盖到您的LoginController
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
还可以从注册表控制器中添加验证程序
受保护的功能,并根据您的字段进行编辑
还要记住编辑用户
模型可填充
数组和创建用户
表的相对迁移文件。将Nullable()
设置为在登录期间不打算输入的字段 在LoginController中实现此方法
protected function attemptLogin(Request $request)
{
// here you can check if user is present or just create.
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
在LoginController中实现此方法
protected function attemptLogin(Request $request)
{
// here you can check if user is present or just create.
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
}
我所要做的就是转向用户模型,不需要指定任何方法
下面是AuthenticatesUsers.php
中的函数的外观:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
//return $this->sendFailedLoginResponse($request);
$user = User::where('email', '=', $_POST['email'])->first();
if ($user === null) {
return User::create([
'name' => $_POST['email'],
'email' => $_POST['email'],
'password' => Hash::make($_POST['password']),
]);
}
else
echo 'Wrong password';
}
我所要做的就是转向用户模型,不需要指定任何方法
下面是AuthenticatesUsers.php
中的函数的外观:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
//return $this->sendFailedLoginResponse($request);
$user = User::where('email', '=', $_POST['email'])->first();
if ($user === null) {
return User::create([
'name' => $_POST['email'],
'email' => $_POST['email'],
'password' => Hash::make($_POST['password']),
]);
}
else
echo 'Wrong password';
}
因此,当用户拼写错误的电子邮件/用户名时,用户将使用拼写错误的电子邮件/用户名登录到新帐户?如果电子邮件/用户名不是唯一的,如果用户拼错密码,他会创建新帐户吗?是的,如果他拼错密码,他将注册为新用户,请不要问我为什么需要这个。电子邮件必须是唯一的,我不会问你为什么需要它,我只是在此警告你,你的用户会讨厌它;-)因此,当用户拼写错误的电子邮件/用户名时,用户将使用拼写错误的电子邮件/用户名登录到新帐户?如果电子邮件/用户名不是唯一的,如果用户拼错密码,他会创建新帐户吗?是的,如果他拼错密码,他将注册为新用户,请不要问我为什么需要这个。电子邮件必须是唯一的,我不会问你为什么需要它,我只是在此警告你,你的用户会讨厌它;-)尝试了类似的操作,但不断出现错误,例如调用未定义的方法illumb\Validation\Validator::make()将其添加到控制器use illumb\Support\Facades\Validator
Illumb\Foundation\Auth\Registered'未找到…当我在单击submit后添加类时,它会写入页面已过期,并将其添加到控制器中use Illumb\Auth\Events\Registered代码>,使用Illumb\Foundation\Auth\RegisterUsers编码>并将此“使用注册表用户;”在类中
。我也尝试了这个方法,得到了“Trait method redirectPath尚未应用,因为在light\Foundation\Auth\AuthenticatesUsers上存在与其他Trait方法的冲突”这是疯狂的尝试类似于此,但不断出现错误,例如调用未定义的方法light\Validation\Validator::make()将其添加到控制器中使用light\Support\Facades\Validator
Illumb\Foundation\Auth\Registered'未找到…当我在单击submit后添加类时,它会写入页面已过期,并将其添加到控制器中use Illumb\Auth\Events\Registered代码>,使用Illumb\Foundation\Auth\RegisterUsers编码>并将此“使用注册表用户;”在类中
。我也尝试了这一点,得到了“Trait method redirectPath尚未应用,因为在light\Foundation\Auth\AuthenticatesUsers上有与其他Trait方法冲突”这是疯狂的我不理解这一点。如果我这样做,它会做什么?类型错误:传递给App\Http\Controllers\Auth\LoginController::attemptLogin()的参数1必须是App\Http\Controllers\Auth\Request的实例,给定的light\Http\Request的实例,在第44行的\vendor\laravel\framework\src\illumb\Foundation\Auth\AuthenticatesUsers.php中调用这是一种登录用户的方法,我想创建一个用户我不理解这一点。如果我这样做,它会做什么?类型错误:传递给App\Http\Controllers\Auth\LoginController::attemptLogin()的参数1必须是App\Http\Controllers\Auth\Request的实例,给定的light\Http\Request的实例,在第44行的\vendor\laravel\framework\src\illumb\Foundation\Auth\AuthenticatesUsers.php中调用这是一种登录用户的方法,我想创建一个用户