laravel / lumen-framework

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

Middleware gets Response object instead of value returned #712

Closed mbkv closed 6 years ago

mbkv commented 6 years ago

Description:

I wanted to have a middleware use the value returned from the controller and format it so we could have metadata alongside the actual return value. But when I tried to get the output of the controller, it returned a response object instead of the Model/Paginator/etc I returned it. I tried to find the location where it's turning it into response, but I got to laravel/lumen-framework/src/Concerns/RoutesRequest.php and then I just got lost between what everything was doing

I know I can get the original value via $response->original. But I was wondering if returning the response was done on purpose or if it was just an oversight

Steps To Reproduce:

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

This returns "Illuminate\Http\Response"

hakuno commented 6 years ago

It's not the purpose.

driesvints commented 6 years ago

You're trying to get the class of the $next param which is the response object so it's normal that you get this back.

mbkv commented 6 years ago

I didn't expect the return value for the controller to be a response object, considering I wasn't returning a response object.

Of course as I said previously, the thing I'm actually looking for is $response->original or $response->getOriginalContent()

Whether or not this was intended, I realized that it's not a good move to change this, because it'll too much of a breaking change

saeednikookalam commented 4 years ago

want you know that response is instanceof which a object?