laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.47k stars 419 forks source link

Fix middleware property setting in controller #1256

Closed wangchristine closed 2 years ago

wangchristine commented 2 years ago

Hi, I found a bug for Lumen 9 version(other version may have same problem) when I was defining middleware in controller. I have a controller like this: app/Http/Controllers/ExampleController.php:

<?php

namespace App\Http\Controllers;

class ExampleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            // some code ...
            return $next($request);
        });
    }
}

And when I'm using app()->make('App\Http\Controllers\ExampleController') for getting this controller middleware property when writing my own package, this got an error:

In Controller.php line 25:

  Illegal offset type

Because this middleware is a Closure. So I changed code and sent this pull request for it. Let me know if I am wrong or correct, thanks.

taylorotwell commented 2 years ago

Maybe Closure middleware like that aren't supported in Lumen? Feels a bit like a breaking change to modify this method?

wangchristine commented 2 years ago

Got it. Thank you for reviewing my pull request. So is it possible support closure middleware in Lumen in future? Why Lumen doesn't support but Laravel does? Is it because creating class instead of using closure causes less memories? Just curious.