如何使用Laravel 5.3注销并重定向到登录页面? [英] How to logout and redirect to login page using Laravel 5.3?

查看:102
本文介绍了如何使用Laravel 5.3注销并重定向到登录页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.3并尝试实现身份验证系统.我使用php artisan命令make:auth进行设置.我根据布局编辑了视图,并将其重定向到我的仪表板页面而不是首页(在安装程序中设置为默认值).现在,当我尝试注销时,抛出此错误

I am using Laravel 5.3 and trying to implement authentication system. I used php artisan command make:auth to setup it. I edited the views according to my layout and redirecting it my dashboard page instead of home (set as default in setup). Now, when I am trying to logout it throwing me this error

NotFoundHttpException in RouteCollection.php line 161

我在routes/web.php中的代码是:

My code in routes/web.php is:

Auth::routes();

Route::get('/pages/superadmin/dashboard', 'HomeController@index');

HomeController.php

<?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;

 class HomeController extends Controller
 {
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    return view('dashboard');
}
}

身份验证/登录Controller.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/dashboard';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

我尝试过此页面上的解决方案:如何设置laravel 5.3注销重定向路径?,但是它不起作用并显示以下错误:

I tried the solutions on this page: How to set laravel 5.3 logout redirect path? but it didn't work and showing these errors:

 ReflectionException in Route.php line 339:
 Class App\Http\Controllers\Auth\Request does not exist

我想将其重定向到auth/文件夹中的登录页面.

I want to redirect it to login page which is in auth/ folder.

推荐答案

在Laravel 5.4中测试

我认为最有效的解决方案是覆盖从 app/Http/Controllers/Auth/LoginController.php 文件内部调用的继承的注销"方法.为此,请向此类添加以下方法:

The solution that I believe works the best is overriding the inherited "logout" method that gets called from inside the app/Http/Controllers/Auth/LoginController.php file. Do this by adding the following method to this class:

/**
 * Log the user out of the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->flush();

    $request->session()->regenerate();

    return redirect('/login');
}

由于"LoginController"类继承自 Illuminate \ Foundation \ Auth \ AuthenticatesUsers ,因此您应该可以安全地覆盖此方法(在LoginController中),而无需编辑实际的供应商文件本身...如果您想升级...,请编辑 AuthenticatesUsers 文件或任何供应商文件,这会引起很多麻烦.

As the "LoginController" class inherits from Illuminate\Foundation\Auth\AuthenticatesUsers, you should safely be able to override this method (in the LoginController) WITHOUT editing the actual vendor file itself... Editing the AuthenticatesUsers file, or any vendor file would cause major headaches down the road if you ever wanted to upgrade...

这里唯一的附加步骤是,您需要在 LoginController 类的顶部包括以下行:

The only additional step here is that you need to include the following line at the top of the LoginController class:

use Illuminate\Http\Request;

这篇关于如何使用Laravel 5.3注销并重定向到登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆