What are rulesets
Rulesets in Guardian allow you to define a series of rules for Rate-Limiting and Error-Handling. Instead of defining rules each time you construct a Guardian instance, you can create pre-determined sets of rules to pass to Guardian. This approach promotes code reusability and cleaner organization of your rate limiting and error handling logic.
Types of Rulesets
Guardian supports two types of rulesets:
Rate Limiting Rulesets
Error Handling Rulesets
Both types of rulesets require you to implement a rules()
method, but they differ in the type of rules they return:
Rate Limiting Rulesets return an array of
RateLimitRule
objectsError Handling Rulesets return an array of
ErrorHandlingRule
objects
Implementing Rate Limiting Rulesets
To create a Rate Limiting Ruleset, your class must implement the Midnite81\Guardian\Contracts\Rulesets\RateLimitingRulesetInterface
. However, for convenience, you can extend the Midnite81\Guardian\Rulesets\AbstractRateLimitingRuleset
class, which implements the interface and provides core logic.
Here's an example of a custom Rate Limiting Ruleset:
Implementing Error Handling Rulesets
For Error Handling Rulesets, your class must implement the Midnite81\Guardian\Contracts\Rulesets\ErrorHandlingRulesetInterface
. Similar to Rate Limiting Rulesets, you can extend the Midnite81\Guardian\Rulesets\AbstractErrorHandlingRuleset
for convenience.
Here's an example of a custom Error Handling Ruleset:
Using Custom Rulesets with Guardian
You can use your custom rulesets with Guardian in two ways:
Pass the ruleset when creating the Guardian instance:
Set the rulesets after creating the Guardian instance:
Adding Additional Rules to a Guardian Instance
In some cases, you might want to add additional rules to a specific Guardian instance without modifying the original ruleset. Guardian provides methods to add rules dynamically:
Adding Rate Limiting Rules
You can use the addRules()
method to add one or more rate limiting rules to an existing Guardian instance:
Adding Error Handling Rules
Similarly, you can use the addErrorRules()
method to add one or more error handling rules:
These methods are useful when you need to:
Add context-specific rules for a particular Guardian instance.
Dynamically adjust rules based on runtime conditions.
Temporarily modify the ruleset for specific operations without affecting the original ruleset.
Remember that rules added this way only apply to the specific Guardian instance and do not modify the original ruleset.
Benefits of Using Rulesets
Reusability: Define rules once and reuse them across multiple Guardian instances.
Organization: Keep your rate limiting and error handling logic separate from your main application code.
Flexibility: Easily switch between different sets of rules for different scenarios or environments.
Maintainability: Update rules in one place, affecting all Guardian instances using that ruleset.
Customization: Ability to add instance-specific rules when needed, without modifying the base ruleset.
By leveraging rulesets and the ability to add rules dynamically, you can create more modular, maintainable, and flexible rate limiting and error handling strategies in your application.
Last updated