简体   繁体   English

登录后如何将用户重定向到同一页面?

[英]How to redirect the user to the same page after login?

I have a login.blade.php, so when user accesses "project.test/login" he has an email and password fields to login. 我有一个login.blade.php,因此,当用户访问“ project.test / login”时,他具有要登录的电子邮件和密码字段。

I also have a header.blade.php that has the main menu, in this main menu there is a link "Create congress". 我也有具有主菜单的header.blade.php,在此主菜单中有一个链接“ Create congress”。 If the user is not authenticated and click in "Create congress" he should not have acess to the page, he should be redirected to the login page. 如果用户未通过身份验证,然后单击“创建会议”,则该用户不需要访问该页面,则应将其重定向到登录页面。 After login with success he should be redirected to the "Create congress" page. 成功登录后,应将他重定向到“创建会议”页面。

This is working fine. 一切正常。

Doubt: 怀疑:

But then I also have a "registration.blade.php" when user accesses " http://project.test/congress/1/congresstest/registration ". 但是当用户访问“ http://project.test/congress/1/congresstest/registration ”时,我还有一个“ registration.blade.php”。 When user accesses this page if is not authenticated it appears this this message: 如果用户未经身份验证访问此页面,则会显示以下消息:

@if(!\Auth::check())
    <div class="alert alert-info" role="alert">
        <span>
            Already have an account? 
            <a data-toggle="modal" 
            data-target=".bd-example-modal-lg" 
            class="font-weight-bold" href="">Login.</a>
        </span>
    </div>
@endif

If user clicks in Login, it appears a modal so the user can insert the email and password and if login with success the modal should close and he should be redirected to the same registration page. 如果用户单击“登录”,则会显示一个模式,以便用户可以输入电子邮件和密码;如果成功登录,则该模式应关闭,并且应将其重定向到同一注册页面。

The issue is that if the user logins with success in this page " http://project.test/congress/1/congresstest/registration " with the form in the modal he is redirected to the "Create congress" page instead of staying in the registration page. 问题是,如果用户使用模式形式的表单成功登录了该页面“ http://project.test/congress/1/congresstest/registration ”,他将被重定向到“创建会议”页面,而不是停留在该页面上注册页面。 Do you know how to correct this issue? 您知道如何解决此问题吗?

Relevant code for the question: 问题的相关代码:

Login.blade.php: Login.blade.php:

<form class="clearfix" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group col-12 px-0">
    <label for="inputEmail4">Email</label>
    <input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus>
</div>
<div class="form-group col-12 px-0">
    <label for="inputEmail4">Password</label>
    <input type="password" class="form-control" name="password" required>
</div>
<button type="submit">Login</button>
</form>

LoginController: 的LoginController:

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';

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

    protected function authenticated(Request $request, $user)
    {
        return redirect()->intended($this->redirectTo);
    }
}

header.blade.php with the main menu where there is a link to create a new congress: header.blade.php及其主菜单,其中包含用于创建新会议的链接:

<li>
    <a href="{!! route('congress.create') !!}"> Create Congress </a>
</li>

Modal with the login form: 使用登录表单的模态:

<div class="modal fade bd-example-modal-lg" id="modal2" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Login</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="container">
          <div class="row">
            <form class="clearfix" method="POST" action="{{ route('login') }}">
              {{ csrf_field() }}

              <div class="form-group col-12 px-0">
                <label for="inputEmail4">Email</label>
                <input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus>
              </div>

              <div class="form-group col-12 px-0">
                <label for="inputEmail4"
                       class="text-heading font-weight-semi-bold">Password
                  <a href="{{ route('password.request') }}" class="text-gray ml-1" style="text-decoration: underline">
                    <small>Recover Password</small></a> </label>
                <input type="password" class="form-control" name="password" required>
              </div>


              <button type="submit" class="btn btn-primary btn d-block w-100">Login</button>

            </form>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>

Routes: 路线:

Route::get('/', [
    'uses' => 'FrontController@index',
    'as'   =>'index'
]);

// Auth routes
Auth::routes();


Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@handleProviderCallback',
]);

// route to the registration page
Route::post('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@storeQuantity',
    'as'   =>'congresses.registration'
]);

 Route::group(['prefix' => '', 'middleware' => 'auth'], function(){

// route to the create congress page
  Route::get('/congress/create', [
        'uses' => 'CongressController@create',
        'as'   => 'congress.create'
    ]);
  }

CongressController: CongressController:

class CongressController extends Controller
{

    public function create()
    {
        return view('congresses.create')->with('categories', Category::all());
    }
}

you need to use session for this, in your congresstest create add the following 您需要为此使用会话,在congresstest中创建以下内容

$uri = $request->path();
$request->session()->put('last_uri', $uri);

then on successful login: 然后成功登录:

protected function authenticated(Request $request, $user)
{    
    $last_uri = $request->session()->get('last_uri');
    // or just simply return redirect()->route('NEEDED URI');
    return redirect()->route($last_uri);
}

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

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