0
votes

I have a pretty straight forward middleware:

protected $auth;

public function __construct(Guard $auth)
{
    $this->auth = $auth;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{

    //dd($this->auth->user());
    if($this->auth->user()->id  && $this->auth->user()->pastDueFees()){
        \Session::flash('message','You must pay past due deal fees before using the rest of the website');
        return redirect()->route('profile.investment-fees');
    } 

    return $next($request);
}

This causes the redirect loop. I am only calling the middleware via Kernel.php.

My Kernal.php:

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

/**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
    'App\Http\Middleware\FeesOwed'
];

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.signed' => 'App\Http\Middleware\AuthenticateSigned',
    'fees' => 'App\Http\Middleware\FeesOwed',
    'auth.subscribed' => 'App\Http\Middleware\AuthenticateSubscribed',
    'admin' => 'App\Http\Middleware\AuthenticateAdmin',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];

}

thanks in advance.

1
What do your routes look like? This middleware isn't applied to the profile.investment-fees route right? - jfadich
Not I am not applying it to routes, just adding it as middleware in Kernal.php - TJ Sherrill
There's your problem. If this is applied to every route then it will be applied to profile.investment-fees as well. When you try to redirect to that route it tries to redirect again because pastDueFees() hasn't changed - jfadich
make sure you added the middleware to only $routeMiddleware and not $middlewareGroups or $middleware - Carlos
There are multiple ways to do it. You could have an extra check in your middleware to not redirect if the current page is the profile.investment-fees page. That might be best if this is meant to be on every single page. Otherwise you could change this to a route middleware like EddyTheDoves answer - jfadich

1 Answers

1
votes

You need to apply that middleware to all routes but profile.investment.fees. In your kernel, add your middleware in the $routeMiddleware array as

'alias' => \App\Http\Middleware\MyMiddleware::class,

Then in your route define a group containing that middleware, and make sure profile.investment-fees is out of it

Route::get('pif', 'MyController@pif')->name('profile.investment-fees');

//Route group
Route::group(['middleware' => 'alias'], function(){
    //every other routes that need the middleware
});

Alternatively, in your middleware, you could simply avoid that specific route by ignoring it with an if else

public function handle(Request $request, Closure $next) {
    if ($request->is('pif')) {
         return $next($request);
    }
    ...
}