generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from tonysm/feature/tm-turbo-native-routes
Adds Turbo Native Navigation Routes and trait
- Loading branch information
Showing
9 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Tonysm\TurboLaravel\Http\Controllers\TurboNativeNavigationController; | ||
|
||
Route::get('recede_historical_location', [TurboNativeNavigationController::class, 'recede'])->name('turbo_recede_historical_location'); | ||
Route::get('resume_historical_location', [TurboNativeNavigationController::class, 'resume'])->name('turbo_resume_historical_location'); | ||
Route::get('refresh_historical_location', [TurboNativeNavigationController::class, 'refresh'])->name('turbo_refresh_historical_location'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel; | ||
|
||
class Features | ||
{ | ||
public static function enabled(string $feature) | ||
{ | ||
return in_array($feature, config('turbo-laravel.features', [])); | ||
} | ||
|
||
public static function turboNativeRoutes(): string | ||
{ | ||
return 'turbo_routes'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Http/Controllers/Concerns/InteractsWithTurboNativeNavigation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Http\Controllers\Concerns; | ||
|
||
trait InteractsWithTurboNativeNavigation | ||
{ | ||
protected function recedeOrRedirectTo(string $url) | ||
{ | ||
return $this->redirectToTurboNativeAction('recede', $url); | ||
} | ||
|
||
protected function resumeOrRedirectTo(string $url) | ||
{ | ||
return $this->redirectToTurboNativeAction('resume', $url); | ||
} | ||
|
||
protected function refreshOrRedirectTo(string $url) | ||
{ | ||
return $this->redirectToTurboNativeAction('refresh', $url); | ||
} | ||
|
||
protected function recedeOrRedirectBack(?string $fallbackUrl, array $options = []) | ||
{ | ||
return $this->redirectToTurboNativeAction('recede', $fallbackUrl, 'back', $options); | ||
} | ||
|
||
protected function resumeOrRedirectBack(?string $fallbackUrl, array $options = []) | ||
{ | ||
return $this->redirectToTurboNativeAction('resume', $fallbackUrl, 'back', $options); | ||
} | ||
|
||
protected function refreshOrRedirectBack(?string $fallbackUrl, array $options = []) | ||
{ | ||
return $this->redirectToTurboNativeAction('refresh', $fallbackUrl, 'back', $options); | ||
} | ||
|
||
protected function redirectToTurboNativeAction(string $action, string $fallbackUrl, string $redirectType = 'to', array $options = []) | ||
{ | ||
if (request()->wasFromTurboNative()) { | ||
return redirect(route("turbo_{$action}_historical_location")); | ||
} | ||
|
||
if ($redirectType === 'back') { | ||
return redirect()->back($options['status'] ?? 302, $options['headers'] ?? [], $fallbackUrl); | ||
} | ||
|
||
return redirect($fallbackUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Http\Controllers; | ||
|
||
use Illuminate\Routing\Controller; | ||
|
||
class TurboNativeNavigationController extends Controller | ||
{ | ||
public function recede() | ||
{ | ||
return response('Going back...'); | ||
} | ||
|
||
public function resume() | ||
{ | ||
return response('Staying put...'); | ||
} | ||
|
||
public function refresh() | ||
{ | ||
return response('Refreshing...'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Tests\Http; | ||
|
||
use Illuminate\Routing\Controller; | ||
use Illuminate\Support\Facades\Route; | ||
use Tonysm\TurboLaravel\Http\Controllers\Concerns\InteractsWithTurboNativeNavigation; | ||
use Tonysm\TurboLaravel\Http\Middleware\TurboMiddleware; | ||
use Tonysm\TurboLaravel\Testing\InteractsWithTurbo; | ||
use Tonysm\TurboLaravel\Tests\TestCase; | ||
|
||
class TurboNativeNavigationControllerTest extends TestCase | ||
{ | ||
use InteractsWithTurbo; | ||
|
||
public function usesTurboNativeRoutes() | ||
{ | ||
Route::middleware(['web', TurboMiddleware::class])->resource('trays', TraysController::class); | ||
} | ||
|
||
public function actionsDataProvider() | ||
{ | ||
return [ | ||
['recede'], | ||
['resume'], | ||
['refresh'], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider actionsDataProvider | ||
* @define-route usesTurboNativeRoutes | ||
*/ | ||
public function recede_resume_or_refresh_when_native_or_redirect_when_not(string $action) | ||
{ | ||
$this->post(route('trays.store'), ['return_to' => "{$action}_or_redirect"]) | ||
->assertRedirect(route('trays.show', 1)); | ||
|
||
$this->turboNative()->post(route('trays.store'), ['return_to' => "{$action}_or_redirect"]) | ||
->assertRedirect(route("turbo_{$action}_historical_location")); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider actionsDataProvider | ||
* @define-route usesTurboNativeRoutes | ||
*/ | ||
public function recede_resume_or_refresh_when_native_or_redirect_back(string $action) | ||
{ | ||
$this->post(route('trays.store'), ['return_to' => "{$action}_or_redirect_back"]) | ||
->assertRedirect(route('trays.show', 5)); | ||
|
||
$this->from(url('/past_place'))->post(route('trays.store'), ['return_to' => "{$action}_or_redirect_back"]) | ||
->assertRedirect(url('/past_place')); | ||
|
||
$this->turboNative()->from(url('/past_place'))->post(route('trays.store'), ['return_to' => "{$action}_or_redirect_back"]) | ||
->assertRedirect(route("turbo_{$action}_historical_location")); | ||
} | ||
|
||
/** | ||
* @test | ||
* @define-route usesTurboNativeRoutes | ||
*/ | ||
public function historical_location_url_responds_with_html() | ||
{ | ||
$this->get(route('turbo_recede_historical_location')) | ||
->assertOk() | ||
->assertSee('Going back...') | ||
->assertHeader('Content-Type', 'text/html; charset=UTF-8'); | ||
|
||
$this->get(route('turbo_resume_historical_location')) | ||
->assertOk() | ||
->assertSee('Staying put...') | ||
->assertHeader('Content-Type', 'text/html; charset=UTF-8'); | ||
|
||
$this->get(route('turbo_refresh_historical_location')) | ||
->assertOk() | ||
->assertSee('Refreshing...') | ||
->assertHeader('Content-Type', 'text/html; charset=UTF-8'); | ||
} | ||
} | ||
|
||
class TraysController extends Controller | ||
{ | ||
use InteractsWithTurboNativeNavigation; | ||
|
||
public function show($trayId) | ||
{ | ||
return [ | ||
'tray_id' => $trayId, | ||
]; | ||
} | ||
|
||
public function store() | ||
{ | ||
return match (request('return_to')) { | ||
'recede_or_redirect' => $this->recedeOrRedirectTo(route('trays.show', 1)), | ||
'resume_or_redirect' => $this->resumeOrRedirectTo(route('trays.show', 1)), | ||
'refresh_or_redirect' => $this->refreshOrRedirectTo(route('trays.show', 1)), | ||
'recede_or_redirect_back' => $this->recedeOrRedirectBack(route('trays.show', 5)), | ||
'resume_or_redirect_back' => $this->resumeOrRedirectBack(route('trays.show', 5)), | ||
'refresh_or_redirect_back' => $this->refreshOrRedirectBack(route('trays.show', 5)), | ||
}; | ||
} | ||
} |