我正在使用Laravel 5.4并尝试实现身份验证系统。我用php artisan命令make:auth来设置它。根据我的布局编辑视图。现在,当我试图注销它扔我这个错误如何使用Laravel 5.4注销并重定向到登录页面?
NotFoundHttpException在RouteCollection.php线161:
可以在任何一个可以帮助我如何注销?
我正在使用Laravel 5.4并尝试实现身份验证系统。我用php artisan命令make:auth来设置它。根据我的布局编辑视图。现在,当我试图注销它扔我这个错误如何使用Laravel 5.4注销并重定向到登录页面?
NotFoundHttpException在RouteCollection.php线161:
可以在任何一个可以帮助我如何注销?
在你web.php
(路线):
地址:
Route::get('logout', '\App\Http\Controllers\Auth\[email protected]');
在你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
,它也应该在你的routes/web.php
路由文件中插入Auth::routes();
。其中默认的logout
路由已经定义并命名为logout
。
你可以see it here on GitHub,但我还会在这里报告的代码简单:
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\[email protected]')->name('login');
$this->post('login', 'Auth\[email protected]');
$this->post('logout', 'Auth\[email protected]')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\[email protected]')->name('register');
$this->post('register', 'Auth\[email protected]');
// Password Reset Routes...
$this->get('password/reset', 'Auth\[email protected]')->name('password.request');
$this->post('password/email', 'Auth\[email protected]')->name('password.email');
$this->get('password/reset/{token}', 'Auth\[email protected]')->name('password.reset');
$this->post('password/reset', 'Auth\[email protected]');
}
然后再请注意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());
不这样做,看到Sid的回应。其他网站可以将用户注销,同时将某人重定向到domain.com/logout – kendepelchin
哦,很酷,很高兴知道 – Beefjeff
如果您使用的身份验证脚手架5.5干脆直接您href
到:
{{ route('logout') }}
无需改变任何路线或控制器。
将您的代码发送至? –