我正在使用Laravel 5.4并尝试实现身份验证系统。我使用php artisan命令make:auth来设置它。我根据我的布局编辑了视图。现在,当我尝试注销时,它会把这个错误抛给我
RouteCollection.php第161行中的NotFoundHttpException:
任何人都可以帮我注销吗?
欢迎各位兄弟 发布技术文章
这里的技术是共享的
我正在使用Laravel 5.4并尝试实现身份验证系统。我使用php artisan命令make:auth来设置它。我根据我的布局编辑了视图。现在,当我尝试注销时,它会把这个错误抛给我
RouteCollection.php第161行中的NotFoundHttpException:
任何人都可以帮我注销吗?
在你的web.php
(路线):
加:
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
在你的 LoginController.php
加:
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
此外,在顶部LoginController.php
,之后namespace
加:
use Auth;
use Illuminate\Http\Request;
现在,您可以使用yourdomain.com/logout
URL 注销,或者如果已创建logout button
,请将href添加到/logout
即使@Tauras的建议正常,我也不认为这是解决这个问题的正确方法。
你说你已经运行了php artisan make:auth
哪些应该也插入Auth::routes();
你的routes/web.php
路由文件中。其中logout
已经定义了默认路由,并且已命名logout
。
你可以在GitHub上看到它,但为了简单起见,我还会在这里报告代码:
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}
然后再请注意,logout
需要 POST
作为HTTP请求方法。这背后有许多有效的原因,但仅提一个非常重要的是,通过这种方式可以防止跨站点请求伪造。
所以根据我刚刚指出的,实现这个的正确方法可能就是这样:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();">
Logout
</a>
<form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
最后请注意,我已经插入laravel开箱即用的功能{{ csrf_field() }}
!
您可以在控制器中使用以下内容:
return redirect('login')->with(Auth::logout());
这是通过在路由中调用Auth :: logout()来实现它的另一种方法
Route::get('/logout', function(){
Auth::logout();
return Redirect::to('login');
});
在5.5
加入
Route::get('logout', 'Auth\LoginController@logout');
到我的路线文件工作正常。
我建议你坚持使用web.php中的Laravel auth路由: Auth::routes()
它将创建以下路线:
POST | logout | App\Http\Controllers\Auth\LoginController@logout
您需要使用POST表单注销。这样您还需要建议的CSRF令牌。
<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit">Logout</button>
</form>
Laravel 5.8的最佳方式
100%工作
在Auth \ LoginController.php中添加此函数
use Illuminate\Http\Request;
并且还添加了这个
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return $this->loggedOut($request) ?: redirect('/login');
}
如果您在5.5中使用auth脚手架,只需将您href
指向:
{{ route('logout') }}
无需改变任何路线或控制器。
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message
- Bariq Dharmawan 于18年3月20日4:32
laravel 5.5注销重定向路径
注销后如何更改重定向路径?