Skip to content

Conversation

@rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Dec 24, 2025

From the default aliased middleware that takes parameters, Illuminate\Auth\Middleware\RedirectIfAuthenticated was the only one missing a static constructor.

Reference:

protected function defaultAliases()
{
$aliases = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \Illuminate\Auth\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => $this->throttleWithRedis
? \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class
: \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
if (class_exists(\Spark\Http\Middleware\VerifyBillableIsSubscribed::class)) {
$aliases['subscribed'] = \Spark\Http\Middleware\VerifyBillableIsSubscribed::class;
}
return $aliases;
}

As such, in codebases that avoid using middleware aliases (preferring to use their FQCN), one would need to manually concatenate a custom guard to the Illuminate\Auth\Middleware\RedirectIfAuthenticated middleware class name, while all others provide a static constructor.

Usage example:

use Illuminate\Auth\Middleware\RedirectIfAuthenticated;
use Illuminate\Routing\Controllers\HasMiddleware;

class ForgotPasswordController implements HasMiddleware
{
    public static function middleware(): array
    {
        return [
            // 'guest:app', // before
            RedirectIfAuthenticated::using('app'), // after
        ];
    }
}

This PR:

  • Adds a static constructor to the Illuminate\Auth\Middleware\RedirectIfAuthenticated middleware.
  • Adds a related test case (based on the analog Illuminate\Auth\Middleware\Authenticate static constructor's test).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant