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 using closures for Statamic route data #9868

Merged
merged 2 commits into from Apr 12, 2024

Conversation

arthurperton
Copy link
Contributor

I love how these Statamic routes are so concise compared to using custom controllers. This PR adds the ability to use a closure for the data argument. So besides this

Route::statamic('uri', 'view', ['foo' => 'bar']);

you can now also do this

Route::statamic('uri', 'view', function () {
    $bar = gatherDataExpensively();

    return ['foo' => $bar];
});

@arthurperton arthurperton changed the title [4.x] Support using closures for Statamic View data [4.x] Support using closures for Statamic route data Apr 12, 2024
@jasonvarga jasonvarga merged commit 316f0be into statamic:4.x Apr 12, 2024
20 checks passed
@arthurperton arthurperton deleted the pr/view-data-from-closure branch April 12, 2024 13:53
@rspenc29
Copy link

rspenc29 commented Apr 23, 2024

what about this?

Route::statamic('things/{thing}', 'things.show', function($thing) {
    $thing = getThing();
    return compact('thing');
});

@jasonvarga
Copy link
Member

Not sure what you're asking.

@rspenc29
Copy link

rspenc29 commented Apr 23, 2024

In laravel, when you use route parameters, they are passed as parameters to the closure. When I use Route::statamic with route parameters, they are not.

https://laravel.com/docs/11.x/routing#route-parameters

I guess my example didn't make much sense now that I read it. Maybe this clears it up.

Route::statamic('things/{id}', 'things.show', function($id) {
    $thing = getThing($id);
    return compact('thing');
});

@jasonvarga
Copy link
Member

Oh yeah sorry now what you said seems obvious. 😊

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

Successfully merging this pull request may close these issues.

None yet

3 participants