Asked  9 Months ago    Answers:  5   Viewed   44 times

I'm trying to add a simple middleware to check if a user matches a role. I'm running into an issue when I use the middleware, I get an Exception:

ReflectionException: class role does not exist

I do not attempt to call a class named role so I assume this is happening magically in Laravel somewhere.

My middleware:

class RoleMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if (! $request->user()->is($role)) {
            return redirect('/login');
        }

        return $next($request);
    }

}

In the users table, I have a role field and in the User model I have:

/**
 * Check if a user is a certain role
 *
 * @param $role
 * @return bool
 */
function is($role) {
    return ($this->role == $role);
}

The route group:

Route::group(['prefix' => 'support', 'middleware' => ['role:admin', 'web']], function() {
        Route::get('threads', 'ThreadController@index');
});

In Http/Kernel.php:

protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        AppHttpMiddlewareVerifyCsrfToken::class,
    ],

    'role' => [
        RoleMiddleware::class,
    ],
];

Anyone have any ideas on what could be causing this ReflectionException?

 Answers

56

This came down to two problems:

  1. $middlewareGroups may not allow for parameters. (Needs to be confirmed) Dropping RoleMiddleware down to $routeMiddleware got rid of the exception.

  2. Having 'web' after 'role:admin' resulted in a null $request->user(). So for future users, you may need to consider placement of your middleware and a check to see if $request->user() is null.

Hope this helps someone else.

Wednesday, March 31, 2021
 
Strae
answered 9 Months ago
46

This may not be the answer you're looking for, but I'd recommend using the now community maintained repository Laravel Collective Forms & HTML as the main repositories have been deprecated.

Laravel Collective is in the process of updating their website. You may view the documentation on GitHub if needed.

Wednesday, March 31, 2021
 
Freddie
answered 9 Months ago
61

This problem is most probably due to a loop in your redirection like you redirect to a route which redirects you back to the same route forming a loop.

The problem seems to be with your logic in IsAdmin middleware. The logic reads If the user is admin then redirect to /admin otherwise pass the request where it is headed which is again /admin route.

This is where the problem is. For instance, you hit middleware.dev/admin it will cross through IsAdminmiddleware and on confirming that user is admin will again redirect it to /adminroute which creates a loop.

Saturday, May 29, 2021
 
gMale
answered 7 Months ago
32

It is Input and not input. This commit removed Input facade definition from config/app.php hence you have to manually add that in to aliases array as below,

'Input' => IlluminateSupportFacadesInput::class,

Or You can import Input facade directly as required,

use IlluminateSupportFacadesInput;
Sunday, June 27, 2021
 
makadev
answered 6 Months ago
93

I believe the correct way to do this (in Laravel 5.x) is to add your custom fields to the attributes property.

From the source code comments, we can see attributes is used for custom parameters:

 /**
 * Custom parameters.
 *
 * @var SymfonyComponentHttpFoundationParameterBag
 *
 * @api
 */
public $attributes;

So you would implement this as follows;

$request->attributes->add(['myAttribute' => 'myValue']);

You can then retrieved the attribute by calling:

Request::get('myAttribute');

Or from request object in laravel 5.5+

 $request->get('myAttribute');
Sunday, July 11, 2021
 
michele
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share