5

I have a middleware called 'AdminMiddleware', which is being used in the constructor of a class. For some reason the middleware is not called from the constructor, although the constructor function is being run. I tried doing a die dump on the adminMiddleware file but it seems like it just ignores this file.

namespace App\Http\Controllers\SuperAdmin;

    use Illuminate\Http\Request;

    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Auth;

        class dashboard extends Controller
        {
            protected $user;

            public function __construct()
                {
                    $this->middleware('admin');
                    $this->user = Auth::User();
                }

//Kernel.php
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'superadmin' => \App\Http\Middleware\SuperAdminMiddleware::class,
        'admin' => \App\Http\Middleware\AdminMiddleware::class,
    ];

For some project requirements i cant use the middleware directly on the routes. Any help is appreciated, i am using laravel 5.1.

Omer Farooq
  • 3,754
  • 6
  • 31
  • 60

4 Answers4

13

I ran into the same issue, and apparently route caching also caches middlewares.

So php artisan route:clear solved it in my case.

Johnny Fekete
  • 281
  • 3
  • 5
9

You need to do 2 things to enable middleware in the controller:

  1. Register middleware in $routeMiddleware in your App\Http\Kernel.php

    protected $routeMiddleware = [ 'admin' => 'App\Http\Middleware\AdminMiddleware', ];

  2. Enable middleware in your controller, using middleware's key, not class name:

    $this->middleware('admin');

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • 2
    well i have added 'admin' => \App\Http\Middleware\AdminMiddleware to the $routeMiddleware array, but it still doesn't work. But it works on the $globalMiddleware array, but once i do that, i cant logout. I have also tried 'admin' => \App\Http\Middleware\AdminMiddleware::class, – Omer Farooq Jul 16 '15 at 13:21
  • You added the middleware, go to any route from this controller and middleware is not executed? – jedrzej.kurylo Jul 16 '15 at 13:32
  • No its not executed when added to the $routeMiddleware, but when added to $middleware array it does executes but then its called on every request. I have made an edit and this is what the controller and kernel looks like now. – Omer Farooq Jul 16 '15 at 16:33
  • The code you pasted works for me. How are you testing that? – jedrzej.kurylo Jul 16 '15 at 16:37
  • well i am just doing a die dump on the middleware, to check if its executed. BTW i am calling the function like this, because i have a wierd senario where i have to do this. return $controller = app()->make('App\Http\Controllers\Admin\dashboard')->index(); – Omer Farooq Jul 16 '15 at 16:46
  • 2
    If you run controller action this way no wonder middleware doesn't get executed. Middleware is called by Laravel kernel when incoming request is processed by Kernel and before the action is called. It won't work if you execute controller method the way you do. – jedrzej.kurylo Jul 16 '15 at 16:57
  • I think i get your point, so is there any other alternative to this? I just want to do a quick check on the constructor to check whether the user has a role of admin. – Omer Farooq Jul 17 '15 at 09:18
  • if (in_array('admin', Auth::user()->roles))? – jedrzej.kurylo Jul 17 '15 at 09:20
  • well actually its not that simple i am using entrust for ACL, so there are some other rules as well to check for. But i guess there is no other way then to hard code them inside the controller. Thanks jedrzej for your answer :) – Omer Farooq Jul 17 '15 at 17:05
  • no problem. but if I were you I'd try to find out how to run the controller the normal way, it will be much simpler then – jedrzej.kurylo Jul 17 '15 at 17:06
  • yes that would be awesome if i could find a simple way to running the controller. If you are interested, here is my problem http://stackoverflow.com/questions/31368433/single-laravel-route-for-multiple-controllers/31368819 – Omer Farooq Jul 20 '15 at 07:13
  • I don't know why when we call full name like $this->middleware(\App\Http\Middleware\Admin::class) it work – Soeurng Sar Nov 01 '17 at 04:07
2

I have the same problem, and solve it by composer dump-autoload. My problem is change the file name and not regenerate the autoload. I hope it works for you.

Alex Chiang
  • 1,800
  • 2
  • 14
  • 21
  • 1
    Thanks Alex for the answer, dont remember what the issue was. But i agree dump autoload does solve most of the problems :P – Omer Farooq Jun 30 '16 at 06:01
0

Using Laravel Framework 9.46.0 doesn't work for me also with the key 'admin', changing it to 'administrator' or 'admin.only' works. it's like 'admin' is a reserved word.