代码之家  ›  专栏  ›  技术社区  ›  SlimenTN

Symfony 4-自动加载器需要在文件中定义类[]

  •  8
  • SlimenTN  · 技术社区  · 7 年前

    我正在尝试添加一个我构建的旧捆绑包 Symfony 3* Symfony 4 但我有一个错误:

    自动加载器需要类 “App\SBC\TiersBundle\Controller\ChantierController”将在中定义 文件 “/Applications/MAMP/htdocs/Projects/HelloSymfony4/vendor/composer/./../src/SBC/TiersBundle/Controller/ChantierController.php”。 已找到文件,但类不在其中,类名或 命名空间可能有输入错误 /应用程序/MAMP/htdocs/项目/HelloSymfony4/配置/服务。亚马尔 (加载到资源中 “/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml”)。

    框架似乎无法识别捆绑包的名称空间,因此我执行了以下步骤:
    在里面 config/bundle.php 我添加了第三行:

    return [
        Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
        \SBC\TiersBundle\TiersBundle::class => ['all' => true], // this one
        Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    ];
    

    并且在 composer.json 我在中添加了第一行 autoload 第节:

    "autoload": {
            "psr-4": {
                "SBC\\": "src/SBC/",
                "App\\": "src/"
    
            }
        },
    

    因为我的包的名称空间以 SBC\ ,我已启动 composer dump-autoload 在控制台中。

    <?php
    
    namespace SBC\TiersBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class TiersBundle extends Bundle
    {
    }
    

    Chantier控制器。php :

    namespace SBC\TiersBundle\Controller;
    
    use SBC\TiersBundle\Entity\Chantier;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\JsonResponse;
    use Symfony\Component\HttpFoundation\Request;
    
    
    class ChantierController extends Controller
    {
    ...
    }
    

    这是我在/src下的包:
    enter image description here

    不幸的是,仍然面临相同的错误,我如何修复它,并提前表示感谢。

    更新 : config/services.yaml :

    # Put parameters here that don't need to change on each machine where the app is deployed
    # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
    parameters:
    
    services:
        # default configuration for services in *this* file
        _defaults:
            autowire: true      # Automatically injects dependencies in your services.
            autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
            public: false       # Allows optimizing the container by removing unused services; this also means
                                # fetching services directly from the container via $container->get() won't work.
                                # The best practice is to be explicit about your dependencies anyway.
    
        # makes classes in src/ available to be used as services
        # this creates a service per class whose id is the fully-qualified class name
        SBC\:
            resource: '../src/SBC/*'
            exclude: '../src/SBC/TiersBundle/{Entity,Migrations,Tests}'
    
        SBC\TiersBundle\Controller\:
            resource: '../src/SBC/TiersBundle/Controller'
            tags: ['controller.service_arguments']
    
        App\:
            resource: '../src/*'
            exclude: '../src/{Entity,Migrations,Tests}'
    
        # controllers are imported separately to make sure services can be injected
        # as action arguments even if you don't extend any base controller class
        App\Controller\:
            resource: '../src/Controller'
            tags: ['controller.service_arguments']
    
        # add more service definitions when explicit configuration is needed
        # please note that last definitions always *replace* previous ones
    
    1 回复  |  直到 7 年前
        1
  •  7
  •   Jakub Krawczyk    7 年前

    该问题很可能是由Symfony配置和名称空间中的冲突引起的。首先,您需要调整 config/services.yaml :

    SBC\:
        resource: '../src/SBC/*'
        exclude: '../src/SBC/TiersBundle/{Entity,Migrations,Tests,Kernel.php}'
    
    SBC\TiersBundle\Controller\:
        resource: '../src/SBC/TiersBundle/Controller'
        tags: ['controller.service_arguments']
    
    App\:
        resource: '../src/*'
        exclude: '../src/{SBC,Entity,Migrations,Tests,Kernel.php}'
    

    这样,您就可以为名称空间定义默认值,并防止使用默认名称空间 App 在生成自动加载类时包含目录。请注意,如果使用注释管线,还需要调整 config/routes/annotations.yaml :

    sbc_controllers:
        resource: ../../src/SBC/TiersBundle/Controller/
        type: annotation
    

    从而正确生成路线。执行这些步骤后,运行 composer dump-autoload 再次清除Symfony的缓存。

    如果将来遇到其他问题,这可能会有所帮助: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md