php 如何设置laravel 5.3注销重定向路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39327970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set laravel 5.3 logout redirect path?
提问by Tim van Uum
Is there no elegant solution to redirect to a specific page after logging out in Laravel 5.3?
在 Laravel 5.3 中注销后,是否没有优雅的解决方案重定向到特定页面?
The function being called is from the trait AuthenticatesUsers:
被调用的函数来自特征AuthenticatesUsers:
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/');
}
This is a default function from the core of laravel. So I have to override the whole function I cannot edit the core. But isn't there a more simpler solution, cause it feel like overkill to manually logout, flush and regenerate again.
这是 laravel 核心的默认函数。所以我必须覆盖整个功能,我无法编辑核心。但是没有更简单的解决方案,因为手动注销,刷新和重新生成感觉有点矫枉过正。
Worked the answers out in an article: https://codeneverlied.com/how-to-set-logout-redirect-path-in-laravel-5-8-and-before/
在一篇文章中找到答案:https: //codeneverlied.com/how-to-set-logout-redirect-path-in-laravel-5-8-and-before/
回答by Avram
This is how I did it. In Auth\LoginController you have:
我就是这样做的。在 Auth\LoginController 你有:
use AuthenticatesUsers;
Change it to:
将其更改为:
use AuthenticatesUsers {
logout as performLogout;
}
Then, define a new logout()
method in your LoginController:
然后,logout()
在 LoginController 中定义一个新方法:
public function logout(Request $request)
{
$this->performLogout($request);
return redirect()->route('your_route');
}
Sure, regular logout()
method in that trait has only 3 lines (used to log users out of the system) so you can copy them to your method, but you should always follow the DRY principle (don't repeat yourself) and re-use as much code as you can.
当然,logout()
该特性中的常规方法只有 3 行(用于将用户从系统中注销),因此您可以将它们复制到您的方法中,但您应该始终遵循 DRY 原则(不要重复自己)并重新用作尽可能多的代码。
回答by Chris
Laravel > 5.7
Laravel > 5.7
The accepted answer is fine, but you can completely bypass touching any of the logout logic by simply overwriting the loggedOut
method:
接受的答案很好,但您可以通过简单地覆盖该loggedOut
方法来完全绕过触及任何注销逻辑:
// App\Http\Controllers\Auth\LoginController.php
protected function loggedOut(Request $request) {
return redirect('/where/ever/you/want/to/go');
}
回答by kalatabe
I would inherit LoginController
and override the logout
function coming from the trait in there:
我会继承LoginController
并覆盖logout
来自那里的特征的函数:
LoginController.php-> leave that as it is.
LoginController.php-> 保持原样。
MyLoginController.php:
MyLoginController.php:
class MyLoginController extends LoginController {
protected $redirectAfterLogout = '/goodbye';
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect($this->redirectAfterLogout);
}
}
Of course, you should remember to update your Auth routes accordingly.
当然,您应该记住相应地更新您的 Auth 路由。
回答by Sachith Muhandiram
I'm using Laravel-5.2, what I used was:
我正在使用Laravel-5.2,我使用的是:
public function logout()
{
Auth::logout();
Session::flush();
return redirect('/');
}
Make sure you have imported:
确保您已导入:
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
In your controller.
在您的控制器中。
回答by Mohamed Gabr
The Auth::routes method in laravel 5.3 registers a POST route for /logout instead of a GET route. This prevents other web applications from logging your users out of your application. To upgrade, you should either convert your logout requests to use the POST verb or just register your own GET route for the /logout URI by adding this route to the file Routes/web.php:-
laravel 5.3 中的 Auth::routes 方法为 /logout 注册了一个 POST 路由,而不是一个 GET 路由。这可以防止其他 Web 应用程序将您的用户从您的应用程序中注销。要升级,您应该将注销请求转换为使用 POST 动词,或者通过将此路由添加到文件 Routes/web.php 来为 /logout URI 注册您自己的 GET 路由:-
Route::get('/logout', 'Auth\LoginController@logout');
and it should work fine and redirect you to the '/' directory as it's defined in the LoginController.php
它应该可以正常工作并将您重定向到“/”目录,因为它在 LoginController.php 中定义
Quoted from:-
引用自:-
回答by S. Domeng
Assuming someone is viewing it now a days and the version of the laravel they are using is 5.7
假设有人现在正在查看它,并且他们使用的 Laravel 版本是 5.7
Add this line in LoginController.php
在 LoginController.php 中添加这一行
public function logout()
{
Auth::logout();
return redirect()->to('/your-route');
}
This assumes you are using the out of the box authentication module provided by laravel
这假设您使用的是 laravel 提供的开箱即用的身份验证模块
回答by Xander
Just use this in routes/web.php
只需在 routes/web.php 中使用它
Route::get('logout', function (){
Auth::logout();
return redirect('your URL');
});
回答by Andrej Ludinovskov
Every logout action fires an event Events\Logout
. You can create a listener that listens to this event and add some logic to there. See more about listeners here https://laravel.com/docs/5.3/events
每个注销操作都会触发一个事件Events\Logout
。您可以创建一个侦听此事件的侦听器并向其中添加一些逻辑。在此处查看有关侦听器的更多信息https://laravel.com/docs/5.3/events
回答by ArtemSky
The simplest way is to override logout trait at LoginController
in App\Http\Controllers\Auth\LoginController
like this
最简单的方法是重写注销特质在LoginController
在App\Http\Controllers\Auth\LoginController
这样的
public function logout(Request $request){
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect()->route('you_route_name');
}
回答by cyclops1101
If you're using the out of the box AuthController add this variable to the top and then change the string to redirect wherever you want.
如果您使用的是开箱即用的 AuthController,请将此变量添加到顶部,然后将字符串更改为重定向到您想要的任何位置。
protected $redirectAfterLogout = '/';
The AuthenticatesUsers class has a logout function that checks for this variable.
AuthenticatesUsers 类具有检查此变量的注销函数。
public function logout()
{
Auth::guard($this->getGuard())->logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}