Laravel 5 - 登录后重定向回上一页

时间:2022-10-20 10:14:02

I have a page with a some content on it and a comments section. Comments can only be left by users who are signed in so I have added a login form to the page for users to sign in with (this only shows if they are not already logged in).

我有一个页面上有一些内容和评论部分。注释只能由已登录的用户保留,因此我已向页面添加登录表单以供用户登录(仅在他们尚未登录时显示)。

The problem I have is that when the user signs in they get redirected back to the home page and not the page they were previously on.

我遇到的问题是,当用户登录时,他们会被重定向回主页,而不是他们之前所在的页面。

I have not changed the login method from the out of the box set-up.

我没有从开箱即用的设置中更改登录方法。

Can anyone suggest a simple way to set the redirect url. My thoughts are that it would be good to be able to set it in the form.

任何人都可以建议一种简单的方法来设置重定向网址。我的想法是能够在表单中设置它会很好。

9 个解决方案

#1


-9  

In

app/Http/Controllers/Auth/AuthController.php

应用程序/ HTTP /控制器/认证/ AuthController.php

add this line and change '/' to your location.

添加此行并将“/”更改为您的位置。

protected $redirectPath = '/';

protected $ redirectPath ='/';

#2


20  

Solution for laravel 5.3:

laravel 5.3的解决方案:

In loginController overwrite the showLoginForm() function as this one:

在loginController中,将showLoginForm()函数覆盖为以下函数:

public function showLoginForm()
{
    if(!session()->has('url.intended'))
    {
        session(['url.intended' => url()->previous()]);
    }
    return view('auth.login');    
}

It will set the "url.intended" session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

它将设置“url.intended”会话变量,即laravel用于在登录后使用前一个URL查找要重定向的页面的变量。

It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

它还检查是否已设置变量,以避免在用户提交带有错误的表单时使用登录URL设置变量。

#3


8  

Please use redirect()->intended() instead in Laravel 5.1

请在Laravel 5.1中使用redirect() - > intention()

You can also see more about it here: http://laravel.com/docs/5.1/authentication

您还可以在此处查看更多相关信息:http://laravel.com/docs/5.1/authentication

#4


7  

For Laravel 5.3

对于Laravel 5.3

inside App/Http/Controllers/Auth/LoginController add this line to the __construct() function

在App / Http / Controllers / Auth / LoginController中将此行添加到__construct()函数中

$this->redirectTo = url()->previous();

So the full code will be

所以完整的代码将是

public function __construct()
{
    $this->redirectTo = url()->previous();
    $this->middleware('guest', ['except' => 'logout']);
}

It works like a charm for me i'm using laravel 5.3.30

它对我来说就像一个魅力我正在使用laravel 5.3.30

#5


4  

Look into laravel cheat sheet

看看laravel备忘单

and use:

并使用:

URL::previous();

to go to the previous page.

转到上一页。

#6


3  

For Laravel 5.4, following code worked for me by just updating LoginController.php

对于Laravel 5.4,以下代码只是通过更新LoginController.php为我工作

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;


public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
    Session::put('backUrl', URL::previous());
}


public function redirectTo()
{
    return Session::get('backUrl') ? Session::get('backUrl') :   $this->redirectTo;
}

#7


0  

Use Thss

使用Thss

return Redirect::back('back-url')

return Redirect :: back('back-url')

#8


0  

For Laravel 5.5, following code worked for me by just updating LoginController.php

对于Laravel 5.5,以下代码只是通过更新LoginController.php为我工作

public function showLoginForm()
{
    session(['link' => url()->previous()]);
    return view('auth.login');
}


protected function authenticated(Request $request, $user)
{
    return redirect(session('link'));
}

#9


-1  

You can use redirect back with Laravel 5:

您可以使用Laravel 5重定向:

<?php namespace App\Http\Controllers;

use Redirect;   
class SomeController extends Controller { 
    public function some_method() {
       return Redirect::back()
    }
}

#1


-9  

In

app/Http/Controllers/Auth/AuthController.php

应用程序/ HTTP /控制器/认证/ AuthController.php

add this line and change '/' to your location.

添加此行并将“/”更改为您的位置。

protected $redirectPath = '/';

protected $ redirectPath ='/';

#2


20  

Solution for laravel 5.3:

laravel 5.3的解决方案:

In loginController overwrite the showLoginForm() function as this one:

在loginController中,将showLoginForm()函数覆盖为以下函数:

public function showLoginForm()
{
    if(!session()->has('url.intended'))
    {
        session(['url.intended' => url()->previous()]);
    }
    return view('auth.login');    
}

It will set the "url.intended" session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

它将设置“url.intended”会话变量,即laravel用于在登录后使用前一个URL查找要重定向的页面的变量。

It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

它还检查是否已设置变量,以避免在用户提交带有错误的表单时使用登录URL设置变量。

#3


8  

Please use redirect()->intended() instead in Laravel 5.1

请在Laravel 5.1中使用redirect() - > intention()

You can also see more about it here: http://laravel.com/docs/5.1/authentication

您还可以在此处查看更多相关信息:http://laravel.com/docs/5.1/authentication

#4


7  

For Laravel 5.3

对于Laravel 5.3

inside App/Http/Controllers/Auth/LoginController add this line to the __construct() function

在App / Http / Controllers / Auth / LoginController中将此行添加到__construct()函数中

$this->redirectTo = url()->previous();

So the full code will be

所以完整的代码将是

public function __construct()
{
    $this->redirectTo = url()->previous();
    $this->middleware('guest', ['except' => 'logout']);
}

It works like a charm for me i'm using laravel 5.3.30

它对我来说就像一个魅力我正在使用laravel 5.3.30

#5


4  

Look into laravel cheat sheet

看看laravel备忘单

and use:

并使用:

URL::previous();

to go to the previous page.

转到上一页。

#6


3  

For Laravel 5.4, following code worked for me by just updating LoginController.php

对于Laravel 5.4,以下代码只是通过更新LoginController.php为我工作

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;


public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
    Session::put('backUrl', URL::previous());
}


public function redirectTo()
{
    return Session::get('backUrl') ? Session::get('backUrl') :   $this->redirectTo;
}

#7


0  

Use Thss

使用Thss

return Redirect::back('back-url')

return Redirect :: back('back-url')

#8


0  

For Laravel 5.5, following code worked for me by just updating LoginController.php

对于Laravel 5.5,以下代码只是通过更新LoginController.php为我工作

public function showLoginForm()
{
    session(['link' => url()->previous()]);
    return view('auth.login');
}


protected function authenticated(Request $request, $user)
{
    return redirect(session('link'));
}

#9


-1  

You can use redirect back with Laravel 5:

您可以使用Laravel 5重定向:

<?php namespace App\Http\Controllers;

use Redirect;   
class SomeController extends Controller { 
    public function some_method() {
       return Redirect::back()
    }
}