Symfony 4-自动加载器预期的类[…]将在文件中定义

Sli*_*nTN 5 php autoload symfony4

我试图将在Symfony 3. *上构建的旧捆绑软件添加到Symfony 4中,但出现此错误:

自动加载器的预期类“ App \ SBC \ TiersBundle \ Controller \ ChantierController”将在文件“ /Applications/MAMP/htdocs/Projects/HelloSymfony4/vendor/composer/../../src/SBC/TiersBundle/Controller/ ChantierController.php”。找到了文件,但没有类,文件名或名称空间可能在/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml中有错字(已加载到资源“ / 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],
];
Run Code Online (Sandbox Code Playgroud)

然后composer.jsonautoload部分中添加第一行:

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

        }
    },
Run Code Online (Sandbox Code Playgroud)

因为我的Bundle的名称空间以开头SBC\,所以我已经composer dump-autoload在控制台中启动了。

<?php

namespace SBC\TiersBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class TiersBundle extends Bundle
{
}
Run Code Online (Sandbox Code Playgroud)

ChantierController.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
{
...
}
Run Code Online (Sandbox Code Playgroud)

这是我在/ src下的捆绑包:
在此处输入图片说明

不幸的是仍然面临着同样的错误,我该如何解决它,并预先感谢。

UPDATEconfig/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
Run Code Online (Sandbox Code Playgroud)

Jak*_*zyk 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}'
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您将为命名空间定义默认值,并App在生成自动加载类时防止默认命名空间包含您的目录。请注意,如果您使用的是注释路由,则还需要调整config/routes/annotations.yaml

sbc_controllers:
    resource: ../../src/SBC/TiersBundle/Controller/
    type: annotation
Run Code Online (Sandbox Code Playgroud)

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

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