Last active
June 20, 2024 19:15
-
-
Save SlimGee/3b870d1f6be9f9b9bdc2f15b2f221a34 to your computer and use it in GitHub Desktop.
Laravel rails like respond_to
This file contains hidden or 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
//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