php - What is the best way to display menu in Laravel 5.1?

129

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 mydefault.blade.php:

@if (Sentry::getUser()->hasAccess('something'))
  <li >
    <a href="{{ URL::to('panda/flot_charts') }}" >
       <span >Menu 1</span><i ></i>
    </a>
    <ul >
    @if (Sentry::getUser()->hasAccess('school'))
      <li active" : '') }}">
         <a href="{{ route('school') }}"><i ></i><span >Submenu</span></a>
      </li>
    @endif
    </ul>
  </li>
@endif

It looks like: screenshot about left menu bar

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 indb?

Thanks for your help!

Peter

284

Answer

Solution:

I useSentinel which is the updated version ofSentry 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 >
    <a href="{{ URL::to('panda/flot_charts') }}" >
       <span >Menu 1</span><i ></i>
    </a>
    <ul >
    @if ($user->hasAccess('school'))
      <li active" : '') }}">
         <a href="{{ route('school') }}"><i ></i><span >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.

People are also looking for solutions to the problem: javascript - extjs Hidden form after download not firing success

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.