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

[4.x] Support route parameters in Statamic routes with closures #9953

Merged
merged 2 commits into from
Apr 24, 2024
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
2 changes: 1 addition & 1 deletion src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function route(Request $request, ...$args)
$params = $request->route()->parameters();
$view = Arr::pull($params, 'view');
$data = Arr::pull($params, 'data');
$data = array_merge($params, is_callable($data) ? $data() : $data);
$data = array_merge($params, is_callable($data) ? $data(...$params) : $data);

$view = app(View::class)
->template($view)
Expand Down
15 changes: 15 additions & 0 deletions tests/Routing/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ protected function resolveApplicationConfiguration($app)

Route::statamic('/route/with/placeholders/{foo}/{bar}/{baz}', 'test');

Route::statamic('/route/with/placeholders/closure/{foo}/{bar}/{baz}', 'test', function ($foo, $bar, $baz) {
return ['hello' => "$foo $bar $baz"];
});

Route::statamic('/route-with-custom-layout', 'test', [
'layout' => 'custom-layout',
'hello' => 'world',
Expand Down Expand Up @@ -141,6 +145,17 @@ public function it_renders_a_view_with_placeholders()
->assertSee('Hello one two three');
}

/** @test */
public function it_renders_a_view_with_placeholders_and_data_from_a_closure()
{
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('test', 'Hello {{ hello }}');

$this->get('/route/with/placeholders/closure/one/two/three')
->assertOk()
->assertSee('Hello one two three');
}

/** @test */
public function it_renders_a_view_with_custom_layout()
{
Expand Down
Loading