Search code examples
laravellaravel-5.4middlewarephp-7

Own RoleMiddleware doesn't get called properly


just some weeks ago I started with Laravel and I made some good progress in these weeks with my project.

3 days ago I decided to implement some roles for my users in the system.

I created a middleware via artisan and wrote the code for it.

The problem I have is to assign my middleware to a specific route. First I added this line to the $routeMiddleware-array in the Kernel.php: 'role' => \App\Http\Middleware\RolesMiddleware::class,.

my routes/web.php-file looks like this on:

// Member area
Route::group(['prefix' => 'member', 'middleware' => ['auth', 'role']], function() {
    Route::get('dashboard', ['as' => 'dashboard', 'uses' => function () {
        return view('member.dashboard');
    }]);

    Route::group(['prefix' => 'user'], function() {
        Route::get('showUsers',[
            'uses' => 'UserController@showUsers',
            'as' => 'manageUsers',
            'roles' => 'manageUsers'
        ]);
    });
});

First I had only the 'auth' middleware in the first group and I wanted to add my 'role' middleware as additional action directly on my get-route like middleware => 'role:manageUsers'. In this case my middleware was ignored totally and did not get called in any case.

After is put the middleware in the array like in the code above it got called at least.

Now I tried to add a custom action 'roles' with the value 'managerUsers' to my get-route. The middleware still gets called and if output the actions via var_dump(request->route()->getAction()); I see the actions 'uses' and 'as' but not my custom action 'roles'.

I have srsly no clue whats wrong. Does anyone know if it isn't possible to add custom actions or an additional middleware to a specific route?

Best regards.

EDIT 1 (17.07.2017)

My \app\Http\Middleware\RolesMiddleware.php looks like this:

namespace App\Http\Middleware;

use Closure;

class RolesMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param \string[] ...$roles
     * @return mixed
     */
    public function handle($request, Closure $next, string ...$roles)
    {
        var_dump($roles);
        die('middleware reachted');
    }
}

My \app\Http\Kernel.php looks like this:

...
protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'role' => \App\Http\Middleware\RolesMiddleware::class,
];
...

And my \routes\web.php look like this:

...
Route::group(['prefix' => 'member', 'middleware' => ['auth', 'role']], function() {
    ...
    Route::group(['prefix' => 'user'], function() {
        Route::get('showUsers',[
            'uses' => 'UserController@showUsers',
            'as' => 'manageUsers'
        ]);
    });
});
...

If I do assign my middleware as an array together with the "auth" middleware, my one will be called. The point is, that I want to define a role the user has to have with a specific route. (In this case with the Route::get(...))

If I do assign my middleware not together with the "auth" middleware (e.g. in the group with prefix "user"), it will be totally ignored. The same if I do assign my middleware with the get-route directly.


Solution

  • As usual the problem was in front of the PC.

    I copied some more routes for later usage and forgot to change the value for 'as' in the action-array.

    Because I'm using the value of the 'as' key for my template to find the correct page I got redirected by another route than expected. Sadly this one pointed to the same controller and action and I didn't noticed the wrong URL in my browsers adress bar.

    My role-middleware worked as expected but I just accessed the wrong page.