简体   繁体   English

在Laravel 5.2视图中使用Auth:User()进行导航

[英]Using Auth:User() in Laravel 5.2 view for nav

I have an index route "/" that loads index.blade.php which extends the master. 我有一个加载index.blade.php的索引路由“ /”,它扩展了主服务器。 I'm unable to use Auth::User() in the nav bar which is a partial included from the master. 我无法在导航栏中使用Auth :: User(),这是母版的一部分。 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. 我有“登录”和“注册”按钮,它们通过/ login和/ register链接AuthController,并希望当经过身份验证的用户查看“ /”索引页面或不需要身份验证的任何其他页面时,将其替换为注销。

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. 当用户登录时,会将/ dashboard路由重定向到可以正常工作的导航栏,并且导航栏会正确显示,但是当他们查看主页时,登录和注册按钮位于导航栏中,单击它们只会将其重定向回到“ /”索引路由,不显示登录或注册表格。 (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. (因为我已经假设他们已经登录了。)再次获取表单的唯一方法是在地址栏中手动输入/ logout。

I've trying looking up answers all over the place but can't figure it out. 我试图在各处查找答案,但无法弄清楚。

dd(\\Auth::user()) returns null in the controller. dd(\\Auth::user())在控制器中返回null。 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. 我见过很多类似的问题,但与Laravel的较早版本有关,但是解决方案似乎无效,或者我只是没有正确地应用它们。

Thanks for the help! 谢谢您的帮助!

route.php route.php

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

PagesController.php 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 app.blade.php

<ul class="nav navbar-nav navbar-right">
      @if (Auth::guest())
        <li><a href="{{ url('/login') }}">Login</a></li>
        <li><a href="{{ url('/register') }}">Register</a></li>
      @else
        <li class="dropdown">
            <a 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 href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
            </ul>
        </li>
     @endif
</ul>

page/index.blade.app 页/ index.blade.app

@extends('layouts.app')

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

Put everything inside the web middleware. 将所有内容放入web中间件中。 The web middleware consists of other middlewares that handles your sessions. web中间件由其他可处理会话的中间件组成。 No session = no user. 没有会话=没有用户。

For example, one of them is called Illuminate\\Session\\Middleware\\StartSession , which is responsible for starting your session. 例如,其中一个名为Illuminate\\Session\\Middleware\\StartSession ,它负责启动会话。

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. 我不确定为什么您决定将特定路由放到web组之外,但是如果没有它,您将没有用户。 You should only put routes outside the group if you plan on doing other things, such as building an API. 如果计划做其他事情,例如构建API,则仅应将路由放置在组外。

You have a backslash before your Auth::user() in your dd(). 您在dd()中的Auth :: user()之前有一个反斜杠。 Also could you replace Auth::guest() with Auth::check() in your app.blade.php? 还可以在app.blade.php中用Auth :: check()替换Auth :: guest()吗?

I had a similar problem. 我有一个类似的问题。 In configured my navbar: 在配置我的导航栏:

@if(Auth::guest())
  <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
     <i class="fa fa-user"></i> <span class="caret"></span>
    </a>
    <ul class="dropdown-menu" role="menu">
      <li><a href="{{ url('/login') }}">Login</a></li>
      <li><a href="{{ url('/register') }}">Sign Up</a></li>
    </ul>
  </li>
@else
  <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
     <i class="fa fa-user"></i> {{ Auth::user()->name }}
                               <span class="caret"></span>
    </a>
    <ul class="dropdown-menu" role="menu">
      <li><a href="{{ url('/logout') }}">Logout</a></li>
    </ul>
 </li>            
@endif

But when I login, this navbar give me error: 但是,当我登录时,此导航栏给我错误:

FatalErrorException in AliasLoader.php line 63: Maximum function nesting level of '100' reached, aborting! AliasLoader.php 63行中的FatalErrorException:达到最大功能嵌套级别'100',正在中止!

To resolv this enter in the bootstrap/autoload.php and add the following line at the end of the file: 要解决此问题,请输入bootstrap / autoload.php并在文件末尾添加以下行:

ini_set('xdebug.max_nesting_level', 120);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM