Laravel登录并注册表单在Laravel 5.2的同一页面上 [英] Laravel login and register forms on the same page in Laravel 5.2

查看:136
本文介绍了Laravel登录并注册表单在Laravel 5.2的同一页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



从Laravel 5.2附带的默认认证和注册系统中,
只是提出了一个简单的看法,这可能是欢迎页面,并包括登录和注册表单。
此时,如果按下任何按钮,验证将检查并从两个表单中找出错误,并将其全部高亮显示出来。
那么包含它们的最佳解决方案是什么?也许是为每个验证单独验证?



我尝试将默认登录系统更改为与寄存器不同形式输入名称,但我认为有一个链,由于验证和输入值添加到数据库。

 < div class =form-group {{$ errors-> has('login_email')?'has-error':''}}> 
< label class =col-md-4 control-label>电子邮件地址< / label>
< div class =col-md-6>
< input type =emailclass =form-controlname =login_emailvalue ={{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>密码< / label>
< div class =col-md-6>
< input type =passwordclass =form-controlname =login_password>
@if($ errors-> has('login_password'))
< span class =help-block>
< strong> {{$ errors-> first('login_password')}}< / strong>
< / span>
@endif
< / div>
< / div>

可以看出,只是将电子邮件和密码输入名称从email更改为login_email '从'密码'到'登录密码'



欢迎任何快速有效的解决方案/想法

解决方案



解决方案

完成这些工作后,我们可以存储一个会话值,告诉我们验证尝试来自登录或注册表单的地方!

 使用AuthenticatesUsers,RegistersUsers {
AuthenticatesUsers :: redirectPath代替RegistersUsers;
AuthenticatesUsers :: getGuard代替RegistersUsers;
登录为traitLogin;
注册为traitRegister;
}

//覆盖trait函数并从overriden函数中调用它
public function login(Request $ request)
{
// Set session作为'login'
Session :: put('last_auth_attempt','login');
//特质不是班级。您无法直接访问其成员。
返回$ this-> traitLogin($ request);


$ b公共函数寄存器(请求$请求)
{
//将会话设置为'register'
Session :: put ('last_auth_attempt','注册');
//特质不是班级。您无法直接访问其成员。
return $ this-> traitRegister($ request);
}

并在您的View.blade文件中,只需使用您的Session值检查您的错误。 ..

 < div class =form-group {{$ errors-> has('email')& & Session :: get('last_auth_attempt')=='login'?'has-error':''}}> 
< label class =col-md-4 control-label>电子邮件< / label>

< div class =col-md-6>
< input type =emailclass =form-controlname =emailvalue ={{old('email')}}>

@if($ errors-> has('email')&& Session:get('last_auth_attempt')=='login')
< span class = 帮助块>
< strong> {{$ errors-> first('email')}}< / strong>
< / span>
@endif
< / div>
< / div>

使用此功能,您可以检查按下的按钮是否来自登录或注册按钮


From default auth and register sistem that comes with Laravel 5.2, Just made a simple view, that could be the welcome page, and to include the login and register forms. At the moment, if any button is pressed, the validation checks and finds errors from both forms, and are highlighted all of them which are required. So what is the best solution to include them and maybe a separate validation for each?

I tried to change the default login sistem to be different from the register form input names, but i think there is a chain, due to validation and adding input values into database.

<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>

As it can be seen, just changed the email and password input names from 'email' to 'login_email' and from 'password' to 'login_password'

Any quick and efficient solutions/ideas are welcome

解决方案

Instead of changing input names, just Override trait function and call it from the overriden function...

With this done, we can store a session value that tell us from where the auth attempt is coming from login or register form!

    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);
}

and in your View.blade file just check your errors with your Session value ...

<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>

With this you can check if button pressed is from login ou register button

这篇关于Laravel登录并注册表单在Laravel 5.2的同一页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆