Ad

Using Auth:User() In Laravel 5.2 View For Nav

I have an index route "/" that loads index.blade.php which extends the master. I'm unable to use Auth::User() in the nav bar which is a partial included from the master. It does work on pages that require authentication. I have Login and Register buttons that link the AuthController via /login and /register and would like those to be replaced by Logout when a authenticated user is viewing the "/" index page or any other page where authentication is not required.

When a user logs in, the are redirected the the /dashboard route which works correctly and the nav bar shows correctly, but when they go an view the homepage, the login and register buttons are in the nav and clicking on them just redirects them back to the "/" index route, not showing a login or register form. (since they're already logged in i'm assuming.) The only way to get a form again is by manually entering /logout in the address bar.

I've trying looking up answers all over the place but can't figure it out.

dd(\Auth::user()) returns null in the controller. I've seen a lot of similar questions but having to do with older versions of Laravel but the solutions don't seem to work or I'm just not applying them correctly.

Thanks for the help!

route.php

Route::get('/','[email protected]');
Route::group(['middleware' => ['web']], function () {
        //    
        Route::controllers([
         'register' => 'Auth\AuthController',
         'password' => 'Auth\PasswordController',
        ]);
});

PagesController.php

class PagesController extends Controller
{
    //

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //

    }

    public function index()
    {

        $nav_active = 'home';

        return view('pages.index',compact('nav_active'));

app.blade.php

<ul class="nav navbar-nav navbar-right">
      @if (Auth::guest())
        <li><a target="_blank" rel="nofollow noreferrer" href="{{ url('/login') }}">Login</a></li>
        <li><a target="_blank" rel="nofollow noreferrer" href="{{ url('/register') }}">Register</a></li>
      @else
        <li class="dropdown">
            <a target="_blank" rel="nofollow noreferrer" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
             {{ Auth::user()->name }} <span class="caret"></span>
            </a>
            <ul class="dropdown-menu" role="menu">
                <li><a target="_blank" rel="nofollow noreferrer" href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
            </ul>
        </li>
     @endif
</ul>

page/index.blade.app

@extends('layouts.app')

@section('content')
<div class="container">
    ...
    </div>
</div>
@stop
Ad

Answer

Put everything inside the web middleware. The web middleware consists of other middlewares that handles your sessions. No session = no user.

For example, one of them is called Illuminate\Session\Middleware\StartSession, which is responsible for starting your session.

I'm not sure why you decided to put that particular route outside the web group, but without it, you won't have a user. You should only put routes outside the group if you plan on doing other things, such as building an API.

Ad
source: stackoverflow.com
Ad