Input/Output Ignoring
Security Control
Prevent accidental leakage of sensitive information
Fine-grained control of data input and output
Data Filtering
Filter fields according to different scenarios
Customize data views for different APIs or user roles
Performance Optimization
Reduce serialization overhead of unnecessary fields
Streamline data transmission
Basic Usage
use Astral\Serialize\Attributes\InputIgnore;
use Astral\Serialize\Attributes\OutputIgnore;
use Astral\Serialize\Serialize;
class User extends Serialize {
public string $name;
// Field ignored during input
#[InputIgnore]
public string $internalId;
// Field ignored during output
#[OutputIgnore]
public string $tempData;
}
// Create user object
$user = User::from([
'name' => 'Job',
'internalId' => 'secret123', // This field will be ignored
'tempData' => 'temporary' // This field will be ignored
]);
echo $user->internalId; // This will output ''
// Convert to array
$userArray = $user->toArray();
// Content of $userArray:
// [
// 'name' => 'John Doe',
// 'internalId' => '',
// ]Ignore by Group
Ignoring by group requires using the Groups annotation together
最后更新于