Installation
Run composer require cmrweb/address-bundle in your Symfony project.
For non-Flex projects, manually add Cmrweb\AddressBundle\AddressBundle::class to config/bundles.php.
First Use Case: Autocomplete Input Generate a search component with:
symfony console make:address -b # With Bootstrap 5 classes
Embed it in Twig:
<twig:SearchAddress />
First Address Retrieval
Use the AddressTrait in a controller or entity:
use Cmrweb\AddressBundle\Traits\AddressTrait;
class MyController {
use AddressTrait;
public function showAddress() {
$address = $this->getAddress(); // Returns Address Model
$addressArray = $this->getAddressArray(); // Returns array
}
}
Frontend Integration
make:address to scaffold autocomplete inputs (supports Bootstrap 5 via -b flag).templates/address/ (override default paths).Backend Integration
AddressTrait to entities or controllers to fetch/save addresses via API.gouv.fr.use Cmrweb\AddressBundle\Traits\AddressTrait;
class User {
use AddressTrait;
// ...
}
API.gouv.fr Integration
config/packages/cmrweb_address.yaml:
cmrweb_address:
api_url: 'https://api-adresse.data.gouv.fr'
Validation & Form Handling
AddressType form field for Symfony forms:
$builder->add('address', AddressType::class);
getAddress().cmrweb_address.yaml).API Rate Limits
cmrweb_address:
cache_enabled: true
cache_lifetime: 3600 # 1 hour
Trait Conflicts
AddressTrait in other bundles. Prefix or namespace traits.Twig Template Overrides
templates/address/ but ensure the directory structure matches the bundle’s (e.g., templates/address/search.html.twig).Bootstrap 5 Flag
-b flag in make:address adds Bootstrap 5 classes. Remove manually if using another CSS framework.var/log/dev.log for API.gouv.fr response errors. Enable debug mode:
cmrweb_address:
debug: true
getAddressArray() returns expected keys (id, name, postcode, etc.). Extend the trait if needed.Custom Address Fields
Address model by creating a custom trait:
trait CustomAddressTrait extends AddressTrait {
public function getFullLabel() {
return $this->getAddressArray()['name'] . ', ' . $this->getAddressArray()['postcode'];
}
}
Alternative APIs
Cmrweb\AddressBundle\Service\AddressService.Validation Rules
AddressType form field to add custom validation:
$builder->add('address', AddressType::class, [
'constraints' => [new NotBlank(), new CustomAddressConstraint()]
]);
How can I help you explore Laravel packages today?