Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel's Middleware not working on Controller

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.

like image 997
Omer Farooq Avatar asked Dec 08 '22 02:12

Omer Farooq


1 Answers

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

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

like image 81
Johnny Fekete Avatar answered Dec 11 '22 10:12

Johnny Fekete