在 Laravel 8 中隐藏冗余 link

Hiding redudant link in Laravel 8

我要隐藏link

@if(Route::has('dashboard.index'))
<li class="nav-item">
    <a class="nav-link" aria-current="page" href="{{ route('dashboard.index') }}">{{__('My Admin')}}</a>
</li>
@endif

如果我注销并重新登录 web.php ,我会使用中间件隐藏路由(例如 authsanctum 和已验证):

Route::middleware(['auth:sanctum', 'verified'])->get(['DashContro::class','index])->name('dashboard.index');

.. 但 Route::has() 仍然是 'true' 在注销时使用什么代替路由可以隐藏 links?

您可以使用以下代码根据登录条件在 blade 中加载 HTML。函数 Auth::check returns 如果用户通过身份验证则为真。

@if(Auth::check())
    // only show this if someone is logged in.
@endif

Route::has 检查路由 是否存在 ,而不是它是否对用户 可访问

相反,您应该使用 Blade 的 @auth@guest 指令来为登录或注销用户显示不同的内容。

https://laravel.com/docs/8.x/blade#authentication-directives

@auth
    // The user is authenticated...
@endauth

@guest
    // The user is not authenticated...
@endguest