Skip to content
Discussion options

You must be logged in to vote

The default request class is macroable.

class Request extends SymfonyRequest implements Arrayable, ArrayAccess
{
use Concerns\InteractsWithContentTypes,
Concerns\InteractsWithFlashData,
Concerns\InteractsWithInput,
Macroable;

So you could simply extend the request class. Put this in the boot method of one of your service providers:

Request::macro('apiVersion', function ()  {
    return request()->header('api-version') ?? 'latest';
});

And then in your controller or wherever you need the api version:

$apiVersion = request()->apiVersion();

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@gthedev
Comment options

@dennisprudlo
Comment options

@gthedev
Comment options

Answer selected by gthedev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants