diff --git a/src/Illuminate/Contracts/Support/Responsable.php b/src/Illuminate/Contracts/Support/Responsable.php new file mode 100644 index 00000000000..26bdb4039dc --- /dev/null +++ b/src/Illuminate/Contracts/Support/Responsable.php @@ -0,0 +1,13 @@ +container->bound(ExceptionHandler::class) || ! $passable instanceof Request) { + if (! $this->container->bound(ExceptionHandler::class) || + ! $passable instanceof Request) { throw $e; } diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index aa393e9ff15..06c7e81c9c1 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -15,6 +15,7 @@ use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\Routing\BindingRegistrar; use Psr\Http\Message\ResponseInterface as PsrResponseInterface; use Illuminate\Contracts\Routing\Registrar as RegistrarContract; @@ -620,6 +621,8 @@ public static function prepareResponse($request, $response) { if ($response instanceof PsrResponseInterface) { $response = (new HttpFoundationFactory)->createResponse($response); + } elseif ($response instanceof Responsable) { + $response = $response->toResponse(); } elseif (! $response instanceof SymfonyResponse && ($response instanceof Arrayable || $response instanceof Jsonable || diff --git a/tests/Integration/Routing/ResponsableTest.php b/tests/Integration/Routing/ResponsableTest.php new file mode 100644 index 00000000000..c565549d308 --- /dev/null +++ b/tests/Integration/Routing/ResponsableTest.php @@ -0,0 +1,33 @@ +get('/responsable'); + + $this->assertEquals(201, $response->status()); + $this->assertEquals('Taylor', $response->headers->get('X-Test-Header')); + $this->assertEquals('hello world', $response->getContent()); + } +} + + +class TestResponsableResponse implements Responsable +{ + public function toResponse() + { + return response('hello world', 201, ['X-Test-Header' => 'Taylor']); + } +}