Symfony 4 - 未找到Symfony \ Component \ Form \ AbstractType

时间:2017-12-17 09:29:26

标签: php symfony

尝试通过文档进行文件上传:

https://symfony.com/doc/current/controller/upload_file.html

我已粘贴代码:

// src/Form/ProductType.php
namespace App\Form;

use App\Entity\Product;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('brochure', FileType::class, array('label' => 'Brochure (PDF file)'))
            // ...
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Product::class,
        ));
    }
}

收到错误

  

未找到类Symfony \ Component \ Form \ AbstractType   E:\ projektai \ PHP   projektai \ htdocs \ mokomieji \ symfony_4_demo \ config / services.yaml(其中   加载在资源“E:\ projektai \ php中   projektai \ htdocs中\ mokomieji \ symfony_4_demo \配置/ services.yaml“)。

我看到PhpStorm已经发出警告,说明找不到AbstractType。这里有什么问题,怎么解决?

composer.json:

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "symfony/console": "^4.0",
        "symfony/flex": "^1.0",
        "symfony/framework-bundle": "^4.0",
        "symfony/lts": "^4@dev",
        "symfony/maker-bundle": "^1.0",
        "symfony/orm-pack": "^1.0",
        "symfony/twig-bundle": "^4.0",
        "symfony/validator": "^4.0",
        "symfony/yaml": "^4.0"
    },
    "require-dev": {
        "symfony/dotenv": "^4.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-apcu": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "id": "01C1AMZG7RRHZQ87EJYRTRWDDA",
            "allow-contrib": false
        }
    }
}

2 个答案:

答案 0 :(得分:4)

可能缺少对symfony/form的依赖?快速浏览一下你列出的软件包及其依赖项让我怀疑......

答案 1 :(得分:2)

你运行composer req form了吗?现在Symfony4是最小的包。以前版本中内置的所有东西,现在你必须通过composer安装。你可以通过两种方式做到这一点。首先使用/ package或使用别名。您可以在symfony.sh上找到所有别名列表。

因此,如果您没有安装表单,可以使用

执行此操作
composer req form

composer req symfony/form