简体   繁体   English

Laravel 5.2 在同一页面上登录和注册表单

[英]Laravel login and register forms on the same page in Laravel 5.2

在同一页面上登录和注册表格

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.从 Laravel 5.2 附带的默认身份验证和注册系统,只是做了一个简单的视图,它可以是欢迎页面,并包含登录和注册表单。 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'可以看出,只是将电子邮件和密码输入名称从“email”更改为“login_email”,将“password”更改为“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 ...并在您的 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">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有了这个,您可以检查按下的按钮是否来自登录或注册按钮

Another quick but maybe not that elegant solution would be inserting a hidden field into the login-form另一个快速但可能不是那么优雅的解决方案是在登录表单中插入一个隐藏字段

<input type="hidden" name="loginform" value="1">

and then just check it with email- and password-errors in both forms.然后用两种形式的电子邮件和密码错误检查它。

At the login-form something like:在登录表单中,类似于:

@if($errors->has('password') && !old('loginform'))

And at the registration-form something like:在注册表中,类似:

@if($errors->has('password') && old('loginform'))

No server-side code needed.不需要服务器端代码。 Laravel version 5.* Laravel 版本 5.*

I figured out: In AuthenticatesUsers trait, change every 'email' to 'login_email' and 'password' to 'login_password'.我想通了:在 AuthenticatesUsers 特征中,将每个“电子邮件”更改为“login_email”,将“密码”更改为“login_password”。 Also, the login attempt will use these as credentials array keys, so edit this function to:此外,登录尝试将使用这些作为凭据数组键,因此将此函数编辑为:

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

Now everything works fine.现在一切正常。

In your AuthController set this in the constructor.在您的 AuthController 中,在构造函数中设置它。

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

You have to send in a hidden input the value of "auth_attempt".您必须在隐藏输入中发送“auth_attempt”的值。 In that way you can know in the view what you were trying to do either login or register.通过这种方式,您可以在视图中了解您尝试登录或注册的操作。

Register form:报名表:

...     

<input type="hidden" name="auth_attempt" value="register">

...

Login form:登录表格:

...     

<input type="hidden" name="auth_attempt" value="login">

...

Then in your validations or where you need it you can use the following to validate if you were trying to login or register.然后在您的验证或您需要的地方,您可以使用以下内容来验证您是否尝试登录或注册。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM