Last active
July 8, 2025 22:17
-
-
Save ziadoz/dd962c017fecb84bbc09d32096cff047 to your computer and use it in GitHub Desktop.
Using Laravel 12's contextual attributes to access request input
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
<?php | |
namespace Illuminate\Container\Attributes; | |
use Attribute; | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Container\ContextualAttribute; | |
// The equivalent of doing `$request->input($key, $default)` in a controller. | |
#[Attribute(Attribute::TARGET_PARAMETER)] | |
class Input implements ContextualAttribute | |
{ | |
/** | |
* Create a new class instance. | |
*/ | |
public function __construct(public ?string $key = null, public mixed $default = null) | |
{ | |
} | |
/** | |
* Resolve the POST data from the request. | |
*/ | |
public static function resolve(self $attribute, Container $container): mixed | |
{ | |
return $container->make('request')->input($attribute->key, $attribute->default); | |
} | |
} |
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
<?php | |
namespace Illuminate\Container\Attributes; | |
use Attribute; | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Container\ContextualAttribute; | |
// The equivalent of doing `$request->post($key, $default)` in a controller. | |
#[Attribute(Attribute::TARGET_PARAMETER)] | |
class Post implements ContextualAttribute | |
{ | |
/** | |
* Create a new class instance. | |
*/ | |
public function __construct(public ?string $key = null, public mixed $default = null) | |
{ | |
} | |
/** | |
* Resolve the POST data from the request. | |
*/ | |
public static function resolve(self $attribute, Container $container): mixed | |
{ | |
return $container->make('request')->post($attribute->key, $attribute->default); | |
} | |
} |
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
<?php | |
namespace Illuminate\Container\Attributes; | |
use Attribute; | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Container\ContextualAttribute; | |
// The equivalent of doing `$request->query($key, $default)` in a controller. | |
#[Attribute(Attribute::TARGET_PARAMETER)] | |
class Query implements ContextualAttribute | |
{ | |
/** | |
* Create a new class instance. | |
*/ | |
public function __construct(public ?string $key = null, public mixed $default = null) | |
{ | |
} | |
/** | |
* Resolve the GET data from the request. | |
*/ | |
public static function resolve(self $attribute, Container $container): mixed | |
{ | |
return $container->make('request')->query($attribute->key, $attribute->default); | |
} | |
} |
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
<?php | |
namespace Illuminate\Container\Attributes; | |
use Attribute; | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Container\ContextualAttribute; | |
use Illuminate\Foundation\Http\FormRequest; | |
// The equivalent of doing `$request->validated($key, $default)` in a controller. | |
#[Attribute(Attribute::TARGET_PARAMETER)] | |
class Validated implements ContextualAttribute | |
{ | |
/** | |
* Create a new class instance. | |
*/ | |
public function __construct(public ?string $key = null, public mixed $default = null) | |
{ | |
} | |
/** | |
* Resolve the POST data from the request. | |
*/ | |
public static function resolve(self $attribute, Container $container): mixed | |
{ | |
return $container->make(FormRequest::class)->validated($attribute->key, $attribute->default); | |
} | |
} |
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
<?php | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Container\ContextualAttribute; | |
use Illuminate\Support\Facades\Route; | |
/** | |
* Create a simple data object that accepts some parameters. | |
* | |
* Tell Laravel which fields in the POST data they should be pulled from via the contextual attribute. | |
*/ | |
class MyUserData | |
{ | |
public function __construct( | |
#[Post('name')] public string $name, | |
#[Post('age')] public int $age, | |
#[Post('hobbies')] public array $hobbies = [], | |
) { | |
} | |
} | |
/** | |
* Create a route that returns a form. | |
*/ | |
Route::get('/user', function () { | |
$token = csrf_token(); | |
return <<<HTML | |
<form method="post"> | |
Name: <input name="name"> | |
Age: <input name="age"> | |
Hobbies: <input name="hobbies[]"><input name="hobbies[]"> | |
<input name="_token" type="hidden" value="$token"> | |
<button>Save</button> | |
</form> | |
HTML; | |
}); | |
/** | |
* Create a route that accepts the data object, and dump it and the request out for comparison. | |
* | |
* In a real application you'd have a form validator to ensure the data is valid. | |
*/ | |
Route::post('/user', function (MyUserData $data) { | |
dump(request()->post(), $data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment