Php Laravel登录并在Laravel 5.2的同一页面上注册表格
从Laravel 5.2附带的默认身份验证和注册系统, 只是做了一个简单的视图,可以是欢迎页面,并包含登录和注册表单。 此时,如果按下任何按钮,验证将检查并发现两个表单中的错误,并突出显示所有必需的错误。 那么,包含它们的最佳解决方案是什么,也许每个都有一个单独的验证 我试图将默认登录系统更改为不同于注册表表单输入名称,但我认为存在一个链,这是由于验证和将输入值添加到数据库中Php Laravel登录并在Laravel 5.2的同一页面上注册表格,php,forms,laravel,login,Php,Forms,Laravel,Login,从Laravel 5.2附带的默认身份验证和注册系统, 只是做了一个简单的视图,可以是欢迎页面,并包含登录和注册表单。 此时,如果按下任何按钮,验证将检查并发现两个表单中的错误,并突出显示所有必需的错误。 那么,包含它们的最佳解决方案是什么,也许每个都有一个单独的验证 我试图将默认登录系统更改为不同于注册表表单输入名称,但我认为存在一个链,这是由于验证和将输入值添加到数据库中 <div class="form-group{{ $errors->has('login_email') ?
<div class="form-group{{ $errors->has('login_email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="login_email" value="{{ old('login_email') }}">
@if ($errors->has('login_email'))
<span class="help-block">
<strong>{{ $errors->first('login_email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('login_password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="login_password">
@if ($errors->has('login_password'))
<span class="help-block">
<strong>{{ $errors->first('login_password') }}</strong>
</span>
@endif
</div>
</div>
电子邮件地址
@如果($errors->has('login\u email'))
{{$errors->first('login_email')}
@恩迪夫
密码
@如果($errors->has('login\u password'))
{{$errors->first('login_password')}
@恩迪夫
可以看到,只需将电子邮件和密码输入名称从“email”更改为“login\u email”,并从“password”更改为“login\u password”
欢迎任何快速有效的解决方案/想法我发现:
在AuthenticateUsers特性中,将每个“电子邮件”更改为“登录电子邮件”,并将“密码”更改为“登录密码”。
此外,登录尝试将使用这些作为凭据数组键,因此请编辑此函数以:
protected function getCredentials(Request $request)
{
$credentials = $request->only($this->loginUsername(), 'login_password');
$credentials['email'] = $credentials['login_email'];
$credentials['password'] = $credentials['login_password'];
unset($credentials['login_email']);
unset($credentials['login_password']);
return $credentials;
//return $request->only($this->loginUsername(), 'login_password');
}
现在一切正常。只需重写trait函数并从重写的函数调用它,而不是更改输入名称 完成此操作后,我们可以存储一个会话值,该值告诉我们身份验证尝试来自登录或注册表单的位置
use AuthenticatesUsers, RegistersUsers {
AuthenticatesUsers::redirectPath insteadof RegistersUsers;
AuthenticatesUsers::getGuard insteadof RegistersUsers;
login as traitLogin;
register as traitRegister;
}
// Override trait function and call it from the overriden function
public function login(Request $request)
{
//Set session as 'login'
Session::put('last_auth_attempt', 'login');
//The trait is not a class. You can't access its members directly.
return $this->traitLogin($request);
}
public function register(Request $request)
{
//Set session as 'register'
Session::put('last_auth_attempt', 'register');
//The trait is not a class. You can't access its members directly.
return $this->traitRegister($request);
}
在View.blade文件中,只需使用会话值检查错误
<div class="form-group{{ $errors->has('email') && Session::get('last_auth_attempt') == 'login' ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email') && Session::get('last_auth_attempt') == 'login')
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
电子邮件
@如果($errors->has('email')&&Session::get('last\u auth\u trunt')=='login')
{{$errors->first('email')}
@恩迪夫
使用此选项,您可以检查按钮是否是从AuthController中的登录ou注册按钮按下的,并在构造函数中设置此选项
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
Session::put('last_auth_attempt', $request->auth_attempt);
}
您必须在一个隐藏的输入中发送“auth_trunt”的值。通过这种方式,您可以在视图中了解登录或注册时要执行的操作
<div class="form-group{{ $errors->has('lastName') && Session::get('last_auth_attempt') == 'register' ? ' has-error' : '' }}">
<label for="name">Last Name</label>
<input id="lastName" type="text" class="form-control" name="lastName" value="{{ old('lastName') }}">
@if ($errors->has('lastName') && Session::get('last_auth_attempt') == 'register')
<span class="help-block">
<strong>{{ $errors->first('lastName') }}</strong>
</span>
@endif
</div>
登记表格:
...
<input type="hidden" name="auth_attempt" value="register">
...
。。。
...
登入表格:
...
<input type="hidden" name="auth_attempt" value="login">
...
。。。
...
然后,在您的验证中,或者在您需要的地方,您可以使用以下内容来验证您是否尝试登录或注册
<div class="form-group{{ $errors->has('lastName') && Session::get('last_auth_attempt') == 'register' ? ' has-error' : '' }}">
<label for="name">Last Name</label>
<input id="lastName" type="text" class="form-control" name="lastName" value="{{ old('lastName') }}">
@if ($errors->has('lastName') && Session::get('last_auth_attempt') == 'register')
<span class="help-block">
<strong>{{ $errors->first('lastName') }}</strong>
</span>
@endif
</div>
姓
@如果($errors->has('lastName')&&Session::get('last\u auth\u trunt')=='register')
{{$errors->first('lastName')}
@恩迪夫
另一个快速但可能不那么优雅的解决方案是在登录表单中插入一个隐藏字段
然后检查两种形式的电子邮件和密码错误
在登录表单中,类似于:
@if($errors->has('password')&&&!old('loginform'))
在登记表上写着:
@if($errors->has('password')&old('loginform'))
不需要服务器端代码。
Laravel版本5.*比如说,您是否更改了
Controller@Method
也是吗?是的,我也做了这些,然后我从SQL中得到了一个错误。它似乎将输入名称与Users表中的列进行比较,但我认为编辑trument()方法的核心是一个好主意,因为在下面的代码中,数组包含这些输入名称:if(Auth::guard($this->getGuard())->trument($credentials,$request->has('memory')){return$this->handleUserWasAuthenticated($request,$throttles);}您应该将此方法复制到AuthController中并在那里编辑它(以覆盖它),但你的代码很有魅力:)@Spidey,你能分享完整的代码吗?我的意思是你在视图、控制器和路由中做了什么?@XahedKamal。代码太长,无法添加到这里。我已添加到以下链接,密码是:laravel@Spidey,谢谢。但你似乎添加了错误的控制器?我没有看到任何showLogin
function@XahedKamal,是相同的函数,只需重命名它。密码:laravelw为什么不使用session()->flash()?