Symfony 4 - 自动加载器期望 class […] 在文件中定义
Symfony 4 - The autoloader expected class […] to be defined in file
我正在尝试将我在 Symfony 3.* 上构建的旧 Bundle 添加到 Symfony 4 但我明白了错误:
The autoloader expected class
"App\SBC\TiersBundle\Controller\ChantierController" to be defined in
file
"/Applications/MAMP/htdocs/Projects/HelloSymfony4/vendor/composer/../../src/SBC/TiersBundle/Controller/ChantierController.php".
The file was found but the class was not in it, the class name or
namespace probably has a typo in
/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml
(which is loaded in resource
"/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/"
}
},
因为我的 Bundle 的命名空间以 SBC\
开头,并且我在控制台中启动了 composer dump-autoload
。
<?php
namespace SBC\TiersBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class TiersBundle extends Bundle
{
}
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
{
...
}
这是我在 /src 下的 Bundle:
不幸的是,仍然面临同样的错误,我该如何解决,在此先感谢。
更新: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
问题很可能是由 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
我正在尝试将我在 Symfony 3.* 上构建的旧 Bundle 添加到 Symfony 4 但我明白了错误:
The autoloader expected class "App\SBC\TiersBundle\Controller\ChantierController" to be defined in file "/Applications/MAMP/htdocs/Projects/HelloSymfony4/vendor/composer/../../src/SBC/TiersBundle/Controller/ChantierController.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml (which is loaded in resource "/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/"
}
},
因为我的 Bundle 的命名空间以 SBC\
开头,并且我在控制台中启动了 composer dump-autoload
。
<?php
namespace SBC\TiersBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class TiersBundle extends Bundle
{
}
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
{
...
}
这是我在 /src 下的 Bundle:
不幸的是,仍然面临同样的错误,我该如何解决,在此先感谢。
更新: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
问题很可能是由 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