我的顶部导航栏中有一个"注销"链接.我想知道怎样才能在我登录时,当我点击它并返回主页时,它会将我注销.

To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?

推荐答案

Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.

1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:

<a href="{{ url('/logout') }}"> logout </a>

然后将其添加到您的web.php文件中

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

Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run

php artisan make:auth 

2) add this to your navigation bar:

<a href="{{ url('/logout') }}"> logout </a>

then add this in your web.php file

Route::get('/logout', 'YourController@logout');

然后在YourController.php文件中添加以下内容

public function logout () {
    //logout user
    auth()->logout();
    // redirect to homepage
    return redirect('/');
}

Done.

阅读:

https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/

Laravel相关问答推荐

从8.0更新到10.0后,图像不再上传到存储

如何提取每组 groupBy 查询的唯一值?

仅针对字母的 laravel 验证规则

Eloquent的条件过滤器

laravel 基本路径

Laravel 5 - Php artisan语法错误

何时在 Laravel 中使用 Repository vs Service vs Trait?

如何更改 ember-cli 中的 dist 文件夹路径?

如何在 Laravel Eloquent 中 Select 某些字段?

在 Laravel 容器中覆盖单例

在 laravel 6.0 中未定义命令ui

Laravel:自定义或扩展通知 - 数据库模型

Blade 中的条件扩展

Laravel 表:只能有一个自动列,并且必须定义为键

Laravel 分页漂亮的 URL

遍历 Laravel 控制器中的结果集

laravel - 无法在控制器构造函数中获取会话

Laravel s3 多桶

开发中的 Laravel 和视图缓存 - 无法立即看到更改

如何访问 Laravel 集合中的第 n 个项目?