链接未显示给auth用户

时间:2018-02-07 12:35:53

标签: laravel

我正在使用laravel auth。当访客用户访问该应用时,我们应该看到链接:

 Item 1, Item 2, Login and Item 3

当他登录时,例如名字John,他应该看到链接:

Item 1, Item 2, John, Item 3

但它不起作用,当用户登录时只显示链接:

John, Item 3

不会出现Item1和Item2。你知道为什么吗?

<ul class="navbar-nav">

  @guest
  <li class="nav-item">
    <a class="nav-link" href="">Item 1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Item 2</a>
  </li>


  <li class="nav-item">
    <a class="nav-link" href="{{ route('login') }}">Login</a>
  </li>
  @else
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="userDropdown"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <i class="fa fa-user" aria-hidden="true"></i> {{ Auth::user()->name }}
    </a>
    <div class="dropdown-menu" aria-labelledby="userDropdown">
      <a class="dropdown-item text-gray" href="{!! url('/item1'); !!}">Login Item 1</a>
      <div class="dropdown-divider text-gray"></div>
      <a class="dropdown-item text-gray" href="{{ route('logout') }}"
         onclick="event.preventDefault();
                  document.getElementById('logout-form').submit();">
        Logout
      </a>
      <form id="logout-form" action="{{ route('logout') }}" method="POST">
        {{ csrf_field() }}
      </form>
    </div>
  </li>
  @endguest

  <li class="nav-item d-none d-lg-block px-0">
    <a class="btn btn-outline-primary btn-sm font-weight-bold" href="">
      Item 3 
    </a>
  </li>
</ul>

3 个答案:

答案 0 :(得分:1)

@guest@else之间的所有内容只有在用户未登录时才会显示。

Item 1指令之后放置Item 2@guest意味着,如果用户未登录,则需要显示Item 1Item 2 < / p>

将代码更改为

  <li class="nav-item">
    <a class="nav-link" href="">Item 1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Item 2</a>
  </li>

  @guest

  <li class="nav-item">
    <a class="nav-link" href="{{ route('login') }}">Login</a>
  </li>

  @else
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="userDropdown"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <i class="fa fa-user" aria-hidden="true"></i> {{ Auth::user()->name }}
    </a>
    <div class="dropdown-menu" aria-labelledby="userDropdown">
      <a class="dropdown-item text-gray" href="{!! url('/item1'); !!}">Login Item 1</a>
      <div class="dropdown-divider text-gray"></div>
      <a class="dropdown-item text-gray" href="{{ route('logout') }}"
         onclick="event.preventDefault();
                  document.getElementById('logout-form').submit();">
        Logout
      </a>
      <form id="logout-form" action="{{ route('logout') }}" method="POST">
        {{ csrf_field() }}
      </form>
    </div>
  </li>
  @endauth

  <li class="nav-item d-none d-lg-block px-0">
    <a class="btn btn-outline-primary btn-sm font-weight-bold" href="">
      Item 3 
    </a>
  </li>
</ul>

您可以阅读有关这些指令的更多信息here

答案 1 :(得分:1)

@guest用于非登录用户。

您应该将其更改为

<ul class="navbar-nav">


  <li class="nav-item">
    <a class="nav-link" href="">Item 1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Item 2</a>
  </li>

  @guest
  <li class="nav-item">
    <a class="nav-link" href="{{ route('login') }}">Login</a>
  </li>
  @endguest

  @if(\Auth::check())
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="userDropdown"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <i class="fa fa-user" aria-hidden="true"></i> {{ Auth::user()->name }}
    </a>
    <div class="dropdown-menu" aria-labelledby="userDropdown">
      <a class="dropdown-item text-gray" href="{!! url('/item1'); !!}">Login Item 1</a>
      <div class="dropdown-divider text-gray"></div>
      <a class="dropdown-item text-gray" href="{{ route('logout') }}"
         onclick="event.preventDefault();
                  document.getElementById('logout-form').submit();">
        Logout
      </a>
      <form id="logout-form" action="{{ route('logout') }}" method="POST">
        {{ csrf_field() }}
      </form>
    </div>
  </li>
  @endif

  <li class="nav-item d-none d-lg-block px-0">
    <a class="btn btn-outline-primary btn-sm font-weight-bold" href="">
      Item 3 
    </a>
  </li>
</ul>

答案 2 :(得分:0)

将其更改为:

<li class="nav-item">
   <a class="nav-link" href="">Item 1</a>
</li>
<li class="nav-item">
   <a class="nav-link" href="#">Item 2</a>
</li>

@guest
    <li class="nav-item">
      <a class="nav-link" href="{{ route('login') }}">Login</a>
    </li>
@endguest

@auth
    <li class="nav-item dropdown">
        ....
    </li>
@endauth


<li class="nav-item d-none d-lg-block px-0">
    <a class="btn btn-outline-primary btn-sm font-weight-bold" href="">
        Item 3 
    </a>
</li>