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

Failed when returned Responsable implementation from a Middleware #24156

Closed
tzsk opened this issue May 9, 2018 · 3 comments
Closed

Failed when returned Responsable implementation from a Middleware #24156

tzsk opened this issue May 9, 2018 · 3 comments

Comments

@tzsk
Copy link

tzsk commented May 9, 2018

  • Laravel Version: 5.6.21
  • PHP Version: 7.1
  • Database Driver & Version: MySQL 5.7

Description:

When you return a Concrete implementation of Illuminate\Contracts\Support\Responsable from a middleware it throws error:

ErrorException (E_NOTICE)
Undefined property: App\TestResponse::$headers

Steps To Reproduce:

1. Create a Response Class:

namespace App;

use Illuminate\Contracts\Support\Responsable;
use Symfony\Component\HttpFoundation\Response;

class TestResponse implements Responsable
{
    public function toResponse($request)
    {
        return response()->json([
            'status' => 'ERROR',
            'code' => Response::HTTP_UNAUTHORIZED,
            'message' => 'Unauthorized Access'
        ]);
    }
}

2. Create a Middleware:

namespace App\Http\Middleware;

use Closure;
use App\TestResponse;

class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!auth()->check()) {
            return new TestResponse;
        }

        return $next($request);
    }
}

3. Add in Kernel:

protected $routeMiddleware = [
    ...
    'test' => \App\Http\Middleware\TestMiddleware::class,
]

4. Add in Route (web.php):

Route::middleware('test')->get('test', function () {
    return view('welcome');
});

5. Now, visit that Route:

ErrorException (E_NOTICE)
Undefined property: App\TestResponse::$headers
@tzsk tzsk changed the title BUG :: Failed when returned Responsable implementation from a Middleware Failed when returned Responsable implementation from a Middleware May 9, 2018
@tillkruss
Copy link
Contributor

Can you submit a PR?

@raheelkhan
Copy link

According to https://laravel-news.com/laravel-5-5-responsable. The object will be converted to HTTP Response when returned from controller or route closure

However, meanwhile it can work like this.

if (!auth()->check()) {
            return (new TestResponse())->toResponse($request);
        }

@tzsk
Copy link
Author

tzsk commented May 9, 2018

@rafaelbeckel Yes. I have already implemented it that way. But, the behavior conflicts with the idea that Responsable implementation is interchangeable with Laravel Response. It should have consistent behavior. Don't you think?

Meanwhile, I am working on a PR to fix this.

TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue May 13, 2018
 - This is a fix for issue laravel#24156 which states that. When we return Implementation of Responsable from middleware it fails.
TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue May 13, 2018
 - This is a fix for issue laravel#24156 which states that. When we return Implementation of Responsable from middleware it fails.
TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue May 13, 2018
 - This is a fix for issue laravel#24156 which states that. When we return Implementation of Responsable from middleware it fails.
TBlindaruk added a commit to TBlindaruk/laravel-framework that referenced this issue May 13, 2018
 - This is a fix for issue laravel#24156 which states that. When we return Implementation of Responsable from middleware it fails.
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

No branches or pull requests

3 participants