daften/addressing-bundle
Composer 安装命令:
composer require daften/addressing-bundle
包简介
Provides an integration between commerceguys addressing package and Symfony.
README 文档
README
Requirements
- jQuery loaded as $
- jQuery Once loaded properly.
Installation
Add the mapping to your doctrine.yaml file:
doctrine: ... orm: ... entity_managers: default: ... mappings: AddressingBundle: is_bundle: true
TODO: Explain why this mapping is needed.
You'll also need to add some configuration or javascript depending on the form you'll use for address information. For
this you need to run bin/console assets:install to copy the bundle assets to the public folder.
AddressEmbeddableType
You'll also need to add some javascript code, to make sure the form changes on changing the country code work.
The script below gives an example. You just need to initialize the javascript functionality. All address fields will automatically be covered. This only works when using Symfony 4 with Webpack Encore.
var countryCodeChange = require('../../public/bundles/addressing/js/countryCodeChange'); countryCodeChange.initialize();
AddressEmbeddableGmapsAutocompleteType
You'll also need to add some javascript code, to make sure the autocomplete functionality works.
The script below gives an example. You just need to initialize the javascript functionality. All autocomplete address fields will automatically be covered. This only works when using Symfony 4 with Webpack Encore.
var addressGmapsAutocomplete = require('../../public/bundles/addressing/js/addressGmapsAutocomplete'); addressGmapsAutocomplete.initialize();
You also need to add the Google API key to the .env file with property key GMAPS_API_KEY. You can override this by overruling the service definition for daften.service.gmaps_autocomplete_service.
Usage
Entity property
You need to add an address field as an ORM Embedded property.
<?php namespace App\Entity; use App\Repository\InstallationAddressRepository; use Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable; use Daften\Bundle\AddressingBundle\Validator\Constraints as AddressingBundleAssert; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: InstallationAddressRepository::class)] class AddressExample { #[ORM\Embedded(class: AddressEmbeddable::class)] #[AddressingBundleAssert\EmbeddedAddressFormatConstraint(fieldOverrides: [ 'addressLine1' => 'required', 'postalCode' => 'required', 'locality' => 'required', 'organization' => 'required', 'givenName' => 'required', 'familyName' => 'required', 'addressLine2' => 'optional', 'additionalName' => 'hidden', 'administrativeArea' => 'hidden', 'dependentLocality' => 'hidden', 'sortingCode' => 'hidden', ])] private AddressEmbeddable $address; /** * AddressExample constructor. */ public function __construct() { $this->address = new AddressEmbeddable(); } /** * @return AddressEmbeddable */ public function getAddress() { return $this->address; } /** * @param AddressEmbeddable $address */ public function setAddress($address): void { $this->address = $address; } }
Entity form
AddressEmbeddableType
There are 3 additional options that can be used for this form type:
- allowed_countries: The countries allowed in the country dropdown. An array where the keys should be the country name and the values should be the 2-character country code.
- preferred_countries: An array with the preferred countries, using the 2-character country codes.
- default_country: The default country to show in the country dropdown.
An example form for the AddressExample class given above using the default AddressEmbeddableType with separate fields.
<?php namespace App\Form; use App\Entity\AddressExample; use Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; /** * Class AddressExampleType */ class AddressExampleType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('address', AddressEmbeddableType::class, [ 'allowed_countries' => [ 'United States' => 'US', 'United Kingdom' => 'UK', 'Belgium' => 'BE', ], 'preferred_countries' => ['BE', 'US'], 'default_country' => 'US', ]); } /** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => AddressExample::class, ]); } }
AddressEmbeddableGmapsAutocompleteType
There is 1 additional option that can be used for this form type:
- allowed_countries: The countries allowed for autocompletion. An array where the values should be the 2-character country code.
An example form for the AddressExample class given above using the AddressEmbeddableGmapsAutocompleteType with one autocomplete field.
<?php namespace App\Form; use App\Entity\AddressExample; use Daften\Bundle\AddressingBundle\Form\Type\AddressEmbeddableGmapsAutocompleteType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; /** * Class AddressExampleType2 */ class AddressExampleType2 extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('address', AddressEmbeddableGmapsAutocompleteType::class, [ 'label' => 'address', 'translation_domain' => 'address', 'allowed_countries' => [ 'BE', 'NL', ], ]); } /** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => AddressExample::class, ]); } }
daften/addressing-bundle 适用场景与选型建议
daften/addressing-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.41k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 03 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「address」 「addressing」 「localization」 「internationalization」 「postal」 「commerceguys」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daften/addressing-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daften/addressing-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daften/addressing-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel package providing addressing functionality
Addressing and zone management for PHP applications.
Addressing and zone management for Symfony applications.
Maps in minutes. Powered by the Google Maps API.
A library that can render international postal addresses in different formats.
Integration of markup/addressing with Symfony2.
统计信息
- 总下载量: 3.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-19