Reputation: 8350
I would like to ask that which is the best way to show menu with access parameters.
So, now I'm using the following code in my default.blade.php
:
@if (Sentry::getUser()->hasAccess('something'))
<li class="{{ (Request::is('panda/something') ? 'open' : '') }}">
<a href="{{ URL::to('panda/flot_charts') }}" class="menu-dropdown">
<span class="menu-text">Menu 1</span><i class="menu-expand"></i>
</a>
<ul class="submenu">
@if (Sentry::getUser()->hasAccess('school'))
<li class="{{ (Request::is('panda/school') ? "active" : '') }}">
<a href="{{ route('school') }}"><i class="menu-icon fa fa-cogs"></i><span class="menu-text">Submenu</span></a>
</li>
@endif
</ul>
</li>
@endif
I know that this solution is not really good. I'm looking for an easier and more simple way to show menu.
I always have to check what page the visitor sees and the same menu has to be active.
Now I'm using Sentry, but I'd like to use middleware in the future.
Shall I store the menu parameters in db
?
Thanks for your help!
Peter
Upvotes: 1
Views: 1467
Reputation: 7975
I use Sentinel
which is the updated version of Sentry
and is also open source. Here's what I use for my authentication middleware:
namespace App\Http\Middleware;
use Closure;
class Auth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($user = Sentinel::check()) {
if ($user->hasAccess(Route::currentRouteAction())) {
view()->share('user', $user);
} else {
return view('errors.forbidden');
}
} else {
return redirect()->guest('/login');
}
return $next($request);
}
}
Then your menu code could be changed to:
@if ($user->hasAccess('something'))
<li class="{{ (Request::is('panda/something') ? 'open' : '') }}">
<a href="{{ URL::to('panda/flot_charts') }}" class="menu-dropdown">
<span class="menu-text">Menu 1</span><i class="menu-expand"></i>
</a>
<ul class="submenu">
@if ($user->hasAccess('school'))
<li class="{{ (Request::is('panda/school') ? "active" : '') }}">
<a href="{{ route('school') }}"><i class="menu-icon fa fa-cogs"></i><span class="menu-text">Submenu</span></a>
</li>
@endif
</ul>
</li>
@endif
This is only a slight change, though. You might want to check out Caffeinated Menus which appears to be the most popular of the package for Laravel 5.1 menu creation on Packalyst.
Upvotes: 1