Home > other >  Why does middleware always fires?
Why does middleware always fires?

Time:09-30

After logging in, my app always goes to the dashboard as intended.

But even after clicking other directories, it always goes to the dashboard.

As I find out, the middleware named RedirectIfAdmin always fires.

I am using hesto/multi-auth.

RedirectIfAdmin:

public function handle($request, Closure $next, $guard = 'admin')
{
   if (Auth::guard($guard)->check()) {
      return redirect('myapp/dashboard');
   }

   return $next($request);
}

On my admin route file:

Route::group(['prefix' => '/myapp', 'as'=>'myapp.'], function () {
  Route::resources([
    'user' => '\App\Http\Controllers\UserController'
    ]);
});

So if I go to myapp/user/ i am always redirected to myapp/dashboard.

I know that it was the code insde the RedirectIfAdmin that is firing when I go to myapp/user because if I change it to myapp/dashboard123 then it goes to that url everytime i browse myapp/user, myapp/user/create, myapp/user/123, myapp/user/123/edit, etc.

Any thoughts?

CodePudding user response:

While calling your myapp/user/ try to call it with

project-url/admin/myapp/user

if you are calling with route then use like

admin.myapp.user

Step 1:

Inside your RouteServiceProvider.php

In that inside map function add this line:

$this->mapWebRoutes();
$this->mapAdminRoutes();

Step 2:

Inside RouteServiceProvider class add this function:

protected function mapAdminRoutes()
    {
        Route::group([
            'middleware' => ['web', 'admin', 'auth:admin'],
            'prefix' => 'admin',
            'as' => 'admin.',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/admin.php');
        });
    }


protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

Step 3: Run below command in your project

php artisan optimize:clear

CodePudding user response:

Check the __construct() on the user controller see if you referenced it there

  • Related