问题
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