问题
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
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');
}
}
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? 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.
回答1:
I finally solved this issue by adding this line in my 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. You can also find it on 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
Hope it helps someone.
Thank You
回答2:
If you want to continue to use GET to logout
Route::get('logout', 'Auth\LoginController@logout');
or
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
回答3:
Tested in 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. 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...
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;
回答4:
In Laravel 5.3 logout is http post instead of http get. You can logout via post request like Taylor Otwell do in 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>
回答5:
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 '/'. 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)
in /routes/web.php add
Route::get('/', function () { return view('home'); })->middleware('auth');
Isn't it the simplest way ?
回答6:
Works on laravel 5.6
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.
来源:https://stackoverflow.com/questions/40114406/how-to-logout-and-redirect-to-login-page-using-laravel-5-3