Skip to content

[12.x] Add static constructor to guest middleware#58204

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
rodrigopedra:guest-middleware
Dec 24, 2025
Merged

[12.x] Add static constructor to guest middleware#58204
taylorotwell merged 1 commit into
laravel:12.xfrom
rodrigopedra:guest-middleware

Conversation

@rodrigopedra

@rodrigopedra rodrigopedra commented Dec 24, 2025

Copy link
Copy Markdown
Contributor

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).

@taylorotwell taylorotwell merged commit 535ef9f into laravel:12.x Dec 24, 2025
72 checks passed
akyrey pushed a commit to akyrey/framework that referenced this pull request Dec 29, 2025
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.

2 participants