How to Use Auth() Register During Login Session in

2019-08-31 02:23发布

问题:

I have changed the register page of Laravel 5.7 Auth() to the studentRegister page to register a new student, but it is inaccessible when I'm logged in through another user. The route didn't work while I'm in the session. However, it works perfectly after logging out from one session.

Please suggest how I can register a new student while I'm logged in.

studentRegister.blade.php

I have removed {{csrf}} as well.

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ __('Register') }}</div>
                    <div class="card-body">
                        <form method="POST" action="{{ route('registerStudent') }}">
                            <div class="form-group row">
                                <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                                <div class="col-md-6">
                                    <input id="name" type="text"
                                           class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}"
                                           name="name" value="{{ old('name') }}" required autofocus>

                                    @if ($errors->has('name'))
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $errors->first('name') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="email"
                                       class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                                <div class="col-md-6">
                                    <input id="email" type="email"
                                           class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}"
                                           name="email" value="{{ old('email') }}" required>

                                    @if ($errors->has('email'))
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $errors->first('email') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password"
                                       class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
                                <div class="col-md-6">
                                    <input id="password" type="password"
                                           class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}"
                                           name="password" required>
                                    @if ($errors->has('password'))
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $errors->first('password') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password-confirm"
                                       class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
                                <div class="col-md-6">
                                    <input id="password-confirm" type="password" class="form-control"
                                           name="password_confirmation" required>
                                </div>
                            </div>

                            <input id="role_id" type="hidden" name="role_id" value="1">

                            <div class="form-group row mb-0">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        {{ __('Register') }}
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

回答1:

You can remove this line from your RegisterController __construct :

$this->middleware('guest');

update You can also define new registration. You can define new route :

Route::get('register2', 'yourController@showRegistrationForm')->name('register2');
Route::post('register2', 'yourController@register');

And in yourController :

public function showRegistrationForm()
{
    return view('auth.register2');
}

public function register(Request $req)
{
    $req->validate([
        'name' => 'required', 'string', 'max:255',
        'email' => 'required', 'string', 'email', 'max:255', 'unique:users',
        'password' => 'required', 'string', 'min:6', 'confirmed',
    ]);

    User::create([
        'name' => $req->name,
        'email' => $req->email,
        'password' => Hash::make($req->password),
    ]);
    return 'something';
}

And copy/paste/rename your register blade to register2 and just change the form action to {{ route('register2') }}



回答2:

go to studentRegisterController.php and delete this function

 public function __construct()
        {
            $this->middleware('guest');
        }

and if you use the default Register go to app/Http/Controller/Auth/RegisterController.php and delete this line

public function __construct()
    {
        $this->middleware('guest');
    }