Laravel 5.2身份验证 – 如何在每个页面中显示登录用户的名称和注销链接?

Laravel 5.2身份验证

我在Laravel 5.2中使用创建了一个新的身份验证脚手架

php artisan make:auth

一切都运作得很完美,除非我在登录时登录后登录/注册链接

/

但是当我在路上时,它会显示用户的姓名,其中有一个注销链接(这是我想要在每个页面中拥有的)

/home

如何在每个页面中显示登录用户的名称和注销链接?

What I don't want when logged in

What I want when logged-in in every page

最佳答案
这是因为,您没有在您/路由上使用auth中间件.在routes.php文件中,默认值为:

Route::get('/', function () {
    return view('welcome');
});

尝试将此闭包移动到生成脚手架时添加的Web中间件.完成后,您的routes.php文件应如下所示:

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

// Route::get('/', function () {
//     return view('welcome');
// });

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    // Add this!
    Route::get('/', function () {
        return view('welcome');
    });

    Route::get('/home', 'HomeController@index');
});

转载注明原文:Laravel 5.2身份验证 – 如何在每个页面中显示登录用户的名称和注销链接? - 代码日志