commerceguys/addressing
Addressing library for handling international postal addresses. Provides country and subdivision data, address formats, validation, and formatting utilities. Useful for commerce, shipping, and any app that needs correct, locale-aware addresses worldwide.
Strengths:
AddressFormatter, AddressValidator) allows selective adoption, reducing bloat in monolithic Laravel apps.HttpClient for geocoding APIs), minimizing external dependencies.Weaknesses:
Validator facade via custom rules (e.g., AddressValidator::validate()).collective/html or Livewire for frontend address fields./api/addresses/validate).street_address, locality, postal_code) but requires manual table design (no migrations included).laravel-http-cache) is recommended.FormRequest or create a custom AddressRule for validation.AddressingServiceProvider) to bind interfaces.@component('address.form')).addresses (e.g., users pivot table with street_address, city, etc.) or use a dedicated addresses table with polymorphic relations.Phase 1: Validation Layer
AddressValidator into Laravel’s validation pipeline.use CommerceGuys\Addressing\Validator\AddressValidator;
$validator = new AddressValidator();
$isValid = $validator->validate($addressString);
use Illuminate\Validation\Rule;
Rule::macro('valid_address', function ($attribute, $value, $fail) {
$validator = new AddressValidator();
if (!$validator->validate($value)) {
$fail('The :attribute is invalid.');
}
});
Phase 2: Formatting & Storage
AddressFormatter to normalize addresses before storage.use CommerceGuys\Addressing\Formatter\AddressFormatter;
$formatter = new AddressFormatter();
$normalized = $formatter->format($addressObject);
Phase 3: Geocoding (Optional)
Geocoder for latitude/longitude (requires Google API key).laravel-http-cache or a custom cache layer.symfony/http-client (for geocoding); ensure no version conflicts.composer.json scripts).composer require commerceguys/addressing.AppServiceProvider or a dedicated service class..env.composer addressing:update-cldr).symfony/http-client or Google API SDKs.composer.json if stability is critical.try {
$validator->validate($address);
} catch (\Exception $e) {
\Log::error("Address validation failed: {$e->getMessage()}");
}
postal_code, city) for query performance.addresses table with full-text search for large datasets.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Google API downtime | Geocoding fails | Fallback to offline CLDR validation. |
| CLDR data corruption | Validation/formatting errors | Validate data integrity on update. |
| Database schema mismatch | Storage/retieval failures | Use migrations and tests for schema changes. |
| High geocoding API costs | Budget overruns | Implement caching and rate limiting. |
| Unsupported locale | Poor address handling | Extend CLDR data or use a hybrid validation. |
How can I help you explore Laravel packages today?