Skip to content

Instantly share code, notes, and snippets.

@SlimGee
Last active June 20, 2024 19:15
Show Gist options
  • Save SlimGee/3b870d1f6be9f9b9bdc2f15b2f221a34 to your computer and use it in GitHub Desktop.
Save SlimGee/3b870d1f6be9f9b9bdc2f15b2f221a34 to your computer and use it in GitHub Desktop.
Laravel rails like respond_to
//AppServiceProvider
/**
* Bootstrap any application services.
*/
public function boot(): void
{
ResponseFactory::macro('to', function (Closure $callback): mixed {
throw_unless($callback, NotFoundHttpException::class);
return app()->call($callback, [
'format' => request()->wasFromTurboFrame() ? 'frame' : request()->format(),
]);
});
}
//helpers.php
if (!function_exists('respond_to')) {
function respond_to(...$args): mixed
{
return response()->to(...$args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment