简体   繁体   English

如何使用Laravel 5.3注销并重定向到登录页面?

[英]How to logout and redirect to login page using Laravel 5.3?

I am using Laravel 5.3 and trying to implement authentication system. 我正在使用Laravel 5.3并尝试实现身份验证系统。 I used php artisan command make:auth to setup it. 我使用php artisan命令make:auth来设置它。 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

My code in routes/web.php is: 我在routes / web.php中的代码是:

Auth::routes();

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

HomeController.php 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');
}
}

Auth/Login Controller.php Auth / Login 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']);
}
}

I tried the solutions on this page: How to set laravel 5.3 logout redirect path? 我尝试了此页面上的解决方案: 如何设置laravel 5.3 logout重定向路径? but it didn't work and showing these errors: 但它没有工作并显示这些错误:

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

I want to redirect it to login page which is in auth/ folder. 我想将其重定向到auth /文件夹中的登录页面。

I finally solved this issue by adding this line in my LoginController.php 我终于在LoginController.php中添加了这一行来解决这个问题

 protected $redirectAfterLogout = 'auth/login';

and editing this file \\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesUsers.php It will use the default '/', if you don't provide $redirectAfterLogout in this file. 并编辑此文件\\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Auth \\ AuthenticatesUsers.php如果您未在此文件中提供$ redirectAfterLogout,它将使用默认的'/'。 You can also find it on github. 你也可以在github上找到它。 Link is at the end of the answer. 链接就在答案的最后。

public function logout()
 {
  return redirect(property_exists($this, 'redirectAfterLogout') ? $this- >redirectAfterLogout : '/');
}

You can also check it here: https://github.com/laravel/framework/commit/aa1204448a0d89e2846cbc383ce487df6efd9fc8#diff-b72935cc9bfd1d3e8139fd163ae00bf5 你也可以在这里查看: https//github.com/laravel/framework/commit/aa1204448a0d89e2846cbc383ce487df6efd9fc8#diff-b72935cc9bfd1d3e8139fd163ae00bf5

Hope it helps someone. 希望它可以帮助某人。

Thank You 谢谢

If you want to continue to use GET to logout 如果您想继续使用GET注销

Route::get('logout', 'Auth\LoginController@logout');

or 要么

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');

Tested in Laravel 5.4 在Laravel 5.4中测试过

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. 我认为最好的解决方案是覆盖从app / Http / Controllers / Auth / LoginController.php文件中调用的继承的“logout”方法。 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');
}

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”类继承自Illuminate \\ Foundation \\ Auth \\ AuthenticatesUsers ,您应该能够安全地覆盖此方法(在LoginController中)而不编辑实际的供应商文件本身...编辑AuthenticatesUsers文件或任何供应商文件如果您想要升级,会导致重大问题...

The only additional step here is that you need to include the following line at the top of the LoginController class: 这里唯一的另一步是你需要在LoginController类的顶部包含以下行:

use Illuminate\Http\Request;

In Laravel 5.3 logout is http post instead of http get . 在Laravel 5.3中, logouthttp post而不是http get You can logout via post request like Taylor Otwell do in auth scaffolding. 您可以通过发布请求注册,例如Taylor Otwell do auth scaffolding。

<a href="{{ url('/logout') }}"
    onclick="event.preventDefault();
             document.getElementById('logout-form').submit();">
    Logout
</a>

<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
    {{ csrf_field() }}
</form>

Redirecting logout action to login page means to me there is no page in the site available unless user is authenticated. 将注销操作重定向到登录页面意味着除非用户通过身份验证,否则站点中没有可用的页面。

I am not a big fan of touching the vendor directory as it is suggested above. 我不是上面提到的触及供应商目录的忠实粉丝。 It is true, Laravel 5.4 vendor/laravel/framework/src/Illuminate/Foundation/Auth/AutenticateUser->logout() redirects to '/' . 确实,Laravel 5.4 vendor/laravel/framework/src/Illuminate/Foundation/Auth/AutenticateUser->logout()重定向到'/' There is no parameter available to change it. 没有可用于更改它的参数。 So let's keep it that way and just add an 'auth protection' to the route '/' (home) 所以,让我们保持这种方式,只需在路线'/'(主页)添加'auth protection'

in /routes/web.php add Route::get('/', function () { return view('home'); })->middleware('auth'); /routes/web.php添加Route::get('/', function () { return view('home'); })->middleware('auth');

Isn't it the simplest way ? 这不是最简单的方法吗?

Works on laravel 5.6 适用于laravel 5.6
File => app/Http/Controllers/Auth/LoginController.php File => app / Http / Controllers / Auth / LoginController.php

use Illuminate\Http\Request;

class LoginController extends Controller
{
    ...  ...  ...
    ...  ...  ...
    ...  ...  ...

    /**
     * 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');
    }
}

Thanks The Virtual Machinist for your answer . 感谢虚拟机械师回答

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

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