Symfony2中全局数据的模式

时间:2013-06-08 16:43:00

标签: symfony

您好我正在开发一个Sf2项目,我在整个应用程序中重新使用一个包含美国州邮政编码的数组 - 在控制器,验证和表单类中。什么是存储此类和类似“全球”数据的最佳位置,以便将其保存在一个位置?我正在考虑扩展Base Controller类并将其放在那里,但我想我会看到社区的想法。谢谢!

2 个答案:

答案 0 :(得分:2)

对于美国州邮政编码,我创建了一个自定义FormType,例如Acme\YourBundle\Form\UsPostalCodeType,可用于创建Symfony的FormBuilder的下拉框。

我有一张我用于英国郡县的名单。

<?php

namespace Acme\SomethingBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class EnglandCountyType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'choices' => self::getCounties()
        ));
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'england_county';
    }

    /**
     * A custom method to apply the values of the array as the keys also and return that array
     *
     * @return array
     */
    public static function getCounties()
    {
        $choices = array(
            "Greater London",
            "West Midlands",
            "Greater Manchester",
            "West Yorkshire",
            "Kent",
            "Essex",
            "Merseyside",
            "South Yorkshire",
            "Hampshire",
            "Lancashire",
            "Surrey",
            "Hertfordshire",
            "Tyne and Wear",
            "Norfolk",
            "Staffordshire",
            "West Sussex",
            "Nottinghamshire",
            "Derbyshire",
            "Devon",
            "Suffolk",
            "Lincolnshire",
            "Northamptonshire",
            "Oxfordshire",
            "Leicestershire",
            "Cambridgeshire",
            "North Yorkshire",
            "Gloucestershire",
            "Worcestershire",
            "Warwickshire",
            "Cornwall",
            "Somerset",
            "East Sussex",
            "County Durham",
            "Buckinghamshire",
            "Cumbria",
            "Wiltshire",
            "Bristol",
            "Dorset",
            "Cheshire East",
            "East Riding of Yorkshire",
            "Leicester",
            "Cheshire West and Chester",
            "Northumberland",
            "Shropshire",
            "Nottingham",
            "Brighton & Hove",
            "Medway",
            "South Gloucestershire",
            "Plymouth",
            "Hull",
            "Central Bedfordshire",
            "Milton Keynes",
            "Derby",
            "Stoke-on-Trent",
            "Southampton",
            "Swindon",
            "Portsmouth",
            "Luton",
            "North Somerset",
            "Warrington",
            "York",
            "Stockton-on-Tees",
            "Peterborough",
            "Herefordshire",
            "Bournemouth",
            "Bath and North East Somerset",
            "Southend-on-Sea",
            "North Lincolnshire",
            "Telford and Wrekin",
            "North East Lincolnshire",
            "Thurrock",
            "Bedford",
            "Reading",
            "Wokingham",
            "West Berkshire",
            "Poole",
            "Blackburn with Darwen",
            "Windsor and Maidenhead",
            "Blackpool",
            "Slough",
            "Middlesbrough",
            "Isle of Wight",
            "Redcar and Cleveland",
            "Torbay",
            "Halton",
            "Bracknell Forest",
            "Darlington",
            "Hartlepool",
            "Rutland",
            "Isles of Scilly",
        );

        asort($choices);

        return array_combine($choices, $choices);
    }
}

要对其进行验证,您可以调用返回选择数组的静态方法作为回调

# src/Acme/SomethingBundle/Resources/config/validation.yml
Acme\SomethingBundle\Entity\Person:
    properties:
        county:
            - Choice: { callback: [\Acme\SomethingBundle\Form\EnglandCountyType, getCounties] }

或在实体中使用注释

use Symfony\Component\Validator\Constraints as Assert;

//....

/**
 * @Assert\Choice(callback={"\Acme\SomethingBundle\Form\EnglandCountyType","getCounties"})
 */
protected $county;

如果您将此用于验证组,则会变为

/**
 * @Assert\Choice(callback={"\Acme\SomethingBundle\Form\EnglandCountyType","getCounties"})
 * @Assert\NotBlank(groups={"group1"})
 */
protected $county;

如果你想在你的控制器中使用它,例如,foreach循环

use Acme\SomethingBundle\Form\EnglandCountyType;

$counties = EnglandCountyType::getCounties();
foreach ($counties as $county) {
    //...
}

答案 1 :(得分:0)

我可能会为它创建一个服务,你可以注入任何需要它的服务。