3 thoughts on “How to use Middleware for content restriction based on user role”

  • 1
    Gagandeep Sharma on November 11, 2017 Reply

    Why u can not control it with just one middleware?

  • 2
    Marco on April 7, 2020 Reply

    Hi. Thank you for this exceptional tutorial, I’m learning Laravel right now and this tutorial is really precious!
    But I’ve got an issue with the code you use in your middlewares. Let’s take AdminMiddleware.php: in the handle function you use this line of code:

    return new Response(view(‘unauthorized’)->with(‘role’, ‘ADMIN’));

    Well, this line raise the following error in my tests:

    Undefined property: Illuminate\Auth\Access\Response::$headers

    So I have replaced it, after a lot of search, with this one:

    return response()->view(‘unauthorized’, [‘role’ => ‘ADMIN’, ‘logged’ => $request->user()->type]);

    Now everything works like a charm but I’m not sure to understand why: as I said , I’m a novice and I still find the internal logic of Laravel a bit confusing 🙂
    Cheers

Leave a Reply