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

[1.x] Allow Fortify views to accept Responsable objects #107

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Http/Responses/SimpleViewResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Fortify\Http\Responses;

use Illuminate\Contracts\Support\Responsable;
use Laravel\Fortify\Contracts\ConfirmPasswordViewResponse;
use Laravel\Fortify\Contracts\LoginViewResponse;
use Laravel\Fortify\Contracts\RegisterViewResponse;
Expand Down Expand Up @@ -45,8 +46,16 @@ public function __construct($view)
*/
public function toResponse($request)
{
return is_callable($this->view) && ! is_string($this->view)
? call_user_func($this->view, $request)
: view($this->view, ['request' => $request]);
if (! is_callable($this->view) || is_string($this->view)) {
return view($this->view, ['request' => $request]);
}

$response = call_user_func($this->view, $request);

if ($response instanceof Responsable) {
return $response->toResponse($request);
}

return $response;
}
}
39 changes: 39 additions & 0 deletions tests/FortifyServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Laravel\Fortify\Tests;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Fortify;

class FortifyServiceProviderTest extends OrchestraTestCase
{
public function test_views_can_be_customized()
{
Fortify::loginView(function () {
return 'foo';
});

$response = $this->get('/login');

$response->assertOk();
$this->assertSame('foo', $response->content());
}

public function test_customized_views_can_return_their_own_responsable()
{
Fortify::loginView(function () {
return new class implements Responsable {
public function toResponse($request)
{
return new JsonResponse(['foo' => 'bar']);
}
};
});

$response = $this->get('/login');

$response->assertOk();
$response->assertExactJson(['foo' => 'bar']);
}
}