在我的導航欄中,我使用 @guest 和 @auth 向客人隱藏一些導航欄鏈接,現在我需要向管理員顯示一些鏈接,如何制作?
管理中間件
public function handle(Request $request, Closure $next)
{
if (Auth::check() && Auth::user()->is_admin == 1) {
return $next($request);
}
return redirect(route('home'));
}
uj5u.com熱心網友回復:
你可以使用 Laravel Gate 去 App/Providers/AuthServiceProvider.php 并在提供者的引導函式中撰寫此代碼它制作一個門
Gate::define("Admin",function(User $user){
if($user->is_admin){
return true;
}
return false;
});
注意:- 使用 Illuminate\Support\Facades\Gate; 在提供者的頂部
現在您的門已準備就緒:- 在您的刀片中,您可以使用 @can 檢查用戶是否為管理員
@can('Admin')
"write something which only admin see"
@endcan
uj5u.com熱心網友回復:
這適用于所有 Laravel 版本:
@if(Auth::check())
// User is authenticated...
@else
// User is not authenticated...
@endif
Laravel > 5.5:
@auth
// user is authenticated...
@endauth
@guest
// User is not authenticated...
@endguest
``
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/323013.html
標籤:拉拉维尔
