Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Using withoutMiddleware() and a closure-based middleware on PHP8 throws an exception #36293

Merged
merged 1 commit into from
Feb 17, 2021
Merged

[8.x] Using withoutMiddleware() and a closure-based middleware on PHP8 throws an exception #36293

merged 1 commit into from
Feb 17, 2021

Conversation

patrickomeara
Copy link
Contributor

@patrickomeara patrickomeara commented Feb 17, 2021

PHP8 throws a TypeError when class_exists() is used on anything but a string.

Psy Shell v0.10.5 (PHP 7.4.13 — cli) by Justin Hileman
>>> class_exists(fn () => []);
<warning>PHP Warning:  class_exists() expects parameter 1 to be string, object given in Psy Shell code on line 1</warning>

Psy Shell v0.10.5 (PHP 8.0.1 — cli) by Justin Hileman
>>> class_exists(fn () => []);
TypeError: class_exists(): Argument #1 ($class) must be of type string, Closure given

This can be replicated by using a closure-based middleware in a controller and removing a class-based middleware from a route.

// app/Http/Kernel.php
class Kernel extends HttpKernel
{
    protected $middleware = [
        \App\Http\Middleware\ContentSecurityPolicy::class,
    ];
}

// app/Http/Controllers/UsersController.php
class UserController extends Controller
{
    /**
     * Instantiate a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            return $next($request);
        });
    }
}

// routes/web.php
Route::get('/users', [UsersController::class, 'list'])
    ->withoutMiddleware(ContentSecurityPolicy::class);

* class_exists on a closure throws an exception in PHP8
@GrahamCampbell GrahamCampbell changed the title Using withoutMiddleware() and a closure-based middleware on PHP8 throws an exception [8.x] Using withoutMiddleware() and a closure-based middleware on PHP8 throws an exception Feb 17, 2021
@GrahamCampbell
Copy link
Member

Is this issue present on 6.x too?

@patrickomeara
Copy link
Contributor Author

patrickomeara commented Feb 17, 2021

I've looked through the code in 6.x and 7.x and it looks like both versions won't have this issue as they don't use class_exists in the gatherRouteMiddleware method.

I've not tried either though.

@patrickomeara
Copy link
Contributor Author

patrickomeara commented Feb 17, 2021

Confirmed, it's not an issue on 6.x (no withoutMiddleware() method) or 7.x (different logic, doesn't use class_exists)

@taylorotwell taylorotwell merged commit 34d58b5 into laravel:8.x Feb 17, 2021
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.

3 participants