Laravel 5.2 Authentication - How can I show logged in user's name and the logout link in every page?

谁都会走 提交于 2019-12-22 11:17:02

问题


Laravel 5.2 Authentication

I created a new authentication scaffolding in Laravel 5.2 using

php artisan make:auth

everything worked perfectly except I get Login/Register links even after logging in when I'm in route

/

but it shows the user's name with a logout link (which is what I want to have in every page) when I'm in route

/home

How can I show logged in user's name and the logout link in every page?


回答1:


This is because, you are not using the auth middleware on you / route. In your routes.php file the default for this is:

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

Try moving this closure into the web middleware that was added in when you generated the scaffolding. Your routes.php file should look something like this when complete:

<?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');
});


来源:https://stackoverflow.com/questions/34468455/laravel-5-2-authentication-how-can-i-show-logged-in-users-name-and-the-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!