php - Symfony2 - 更改包名称后的 Twig 异常

标签 php symfony twig

我在 Symfony 2.6.13 中遇到问题。 我创建了一个自定义类型 GeneralBundle/Form/Type/DateRangeType.php:

<?php

namespace Ironstat\GeneralBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DateRangeType extends AbstractType
{
    const NAME = 'date_range';

    private $manager;

    public function __construct($manager) {
            $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('dateFrom', 'date', array(
                'widget'        =>  'single_text',
                'format'        => 'dd/MM/yyyy',
                'attr'      => array('class' => 'datepicker input-small'),
                'label_attr'    => array('class' => 'control-label label-sm'),
                'label'     => 'Fecha desde',
                'empty_value'   => false,
            ))
            ->add('dateTo', 'date', array(
                'widget'        =>  'single_text',
                'format'        => 'dd/MM/yyyy',
                'attr'      => array('class' => 'datepicker input-small'),
                'label_attr'    => array('class' => 'control-label label-sm'),
                'label'     => 'Fecha hasta',
                'empty_value'   => false
            ));

            $transformer = new DateRangeTransform($this->manager);

            $builder->addModelTransformer($transformer);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Ironstat\GeneralBundle\Entity\DateRange',
        ));
    }

    public function getName() {
        return self::NAME;
    }
}

因此,根据文档,我还将服务创建到 GeneralBundle/Resources/config/services.yml 中:

services:
    ironstat.type.date_range:
        class: Ironstat\GeneralBundle\Form\Type\DateRangeType
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: form.type, alias: date_range }

我还在 config.yml 中添加了这个配置:

twig:
    form:
        resources:
            - 'IronstatGeneralBundle:Form:Type:DateRangeType'

但是,当我尝试将它添加到我的表单中时,出现了这个错误:

An exception has been thrown during the rendering of a template ("Could not load type "date_range"") in IronstatPacienteBundle:Page:edit.html.twig at line 27.

在我的表单中,我是这样添加的:

$builder->add('cau'     , 'collection'  , $this->getDateRangeType());

最后一个重要的信息是它在对包的名称进行重构 后停止工作(是的,我是个傻瓜,我知道...)。 在重构之前它工作正常。进行重构后(我在整个项目中将 neostat 更改为 ironstat),除此之外一切正常。我删除了缓存并重新生成了 bootstrap.php.cache,但它仍然不起作用。 我还确保做好重构。在整个项目中找到“Neostat”或“neostat”并没有给我带来结果。

缺少什么?

非常感谢。


更新:这是我的 appKernel.php:

public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new Ironstat\PacienteBundle\IronstatPacienteBundle(),
            new Ironstat\DiagnosticoBundle\IronstatDiagnosticoBundle(),
            new Ironstat\EntidadBundle\IronstatEntidadBundle(),
            new Ironstat\usuarioBundle\IronstatusuarioBundle(),
            new Ironstat\GeneralBundle\IronstatGeneralBundle(),
            new Siphoc\PdfBundle\SiphocPdfBundle(),
            new FOS\UserBundle\FOSUserBundle(),
            new Ironstat\EnvioBundle\IronstatEnvioBundle(),
            new Ironstat\ArchivoBundle\IronstatArchivoBundle(),
            new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
            new Ironstat\ReporteBundle\IronstatReporteBundle(),
            new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),
        );

这是 getDateRangeType 函数:

private function getDateRangeType() {
        return array(
            'type'      => 'date_range',
            'allow_add'     => true,
            'allow_delete'  => true,
            'attr'      => array('class' => 'datepicker input-small'),
            'label_attr'    => array('class' => 'control-label'),
            'empty_data'    => null
            );
    }

最佳答案

我找到了解决方案: 问题是它缺少重命名文件(重构不太好,大声笑)。 显然,我需要重命名捆绑配置文件。 我注意到当我运行这个命令控制台时:

find ./ -name "Neostat*"

文件列表是:

./Symfony/src/Ironstat/ReporteBundle/DependencyInjection/NeostatReporteExtension.php ./Symfony/src/Ironstat/usuarioBundle/DependencyInjection/NeostatusuarioExtension.php ./Symfony/src/Ironstat/ArchivoBundle/DependencyInjection/NeostatArchivoExtension.php ./Symfony/src/Ironstat/PacienteBundle/DependencyInjection/NeostatPacienteExtension.php ./Symfony/src/Ironstat/DiagnosticoBundle/DependencyInjection/NeostatDiagnosticoExtension.php ./Symfony/src/Ironstat/archivoBundle/DependencyInjection/NeostatarchivoExtension.php ./Symfony/src/Ironstat/envioBundle/DependencyInjection/NeostatenvioExtension.php ./Symfony/src/Ironstat/EnvioBundle/DependencyInjection/NeostatEnvioExtension.php ./Symfony/src/Ironstat/EntidadBundle/DependencyInjection/NeostatEntidadExtension.php

更改这些文件的名称是有效的。

非常感谢。

关于php - Symfony2 - 更改包名称后的 Twig 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793460/

相关文章:

php - PHP 和 MySQL 中的日期和时间函数

php - 更改了服务器,现在密码以不同的方式散列 - 旧的不再有效

php - 学说可以有多个到数据库的连接吗?

php - 如何在 Doctrine 中的多对多 JoinTable 上添加索引定义?

php - 如何获取/打印对我的 Twig 模板 (Drupal 8) 的实体引用的路径或 url?

javascript - opencart v3中猫头鹰轮播的使用

php - Magento:当我尝试在主页上显示最畅销的产品时,$this->getAddToCartUrl($product) 不起作用

php - Tuleap:无法在 Tracker 中附加文件

php - 让 Symfony 2 Assetic 开发变得舒适

css - Symfony2 Twig 模板 - CSS 未加载