Reputation: 1421
I'm building an app where I have to show different navigation for users role. However, this is what I've came up with
Navigation example :
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
@if (Auth::check() && Auth::user()->role == 'author')
<li><a href="{{ url('user/' . Auth::user()->nameslug) }}"><i class="fa fa-user"></i> {{ Auth::user()->name }}</a></li>
@endif
@if (Auth::check() && Auth::user()->role == 'admin')
@if (!empty($page) && $page == 'admin')
<li class="active"><a href="{{ url('admin') }}"><i class="fa fa-user"></i> Admin</a></li>
@else
<li><a href="{{ url('admin') }}"><i class="fa fa-user"></i> Admin</a></li>
@endif
@endif
@if (Auth::check())
<li><a href="{{ url('logout') }}"><i class="fa fa-sign-out"></i> Logout</a></li>
@else
@if (!empty($page) && $page == 'login')
<li class="active"><a href="{{ url('login') }}"><i class="fa fa-sign-in"></i> Login</a></li>
@else
<li><a href="{{ url('login') }}"><i class="fa fa-sign-in"></i> Login</a></li>
@endif
@endif
</ul>
</div><!--/.nav-collapse -->
I've tested it and it works just fine, but I'm just wondering if this will affect the app performance, making it slow ? If so is there a better way to do this ? That's all and thanks!
Upvotes: 1
Views: 713
Reputation: 33186
This should not impact your application speed. A few if/else
blocks won't change much. The roles for this user will be loaded on every page, but this is probably a very light query and Laravel is smart enough to only execute this query once per request.
So conclusion: this looks fine. :)
Upvotes: 2