Php Symfony:未正确加载带名称空间的捆绑包
我有一个新安装的(通过composer)Symfony应用程序。我移动了一些东西,似乎这导致了Symfony的控制器注释驱动程序崩溃。最值得注意的是,我使用了一个名称空间 我的Php Symfony:未正确加载带名称空间的捆绑包,php,symfony,configuration,Php,Symfony,Configuration,我有一个新安装的(通过composer)Symfony应用程序。我移动了一些东西,似乎这导致了Symfony的控制器注释驱动程序崩溃。最值得注意的是,我使用了一个名称空间 我的app/config/routing.yml未修改。即: app: resource: '@Kafoso/TestApp/AppBundle/Controller/' type: annotation 而app/config/routing_dev.yml指向app/config/routing.yml
app/config/routing.yml
未修改。即:
app:
resource: '@Kafoso/TestApp/AppBundle/Controller/'
type: annotation
而app/config/routing_dev.yml
指向app/config/routing.yml
现在,我已经将AppBundle
类移动到了一个新的名称空间(以及文件夹位置)Kafoso\TestApp\AppBundle
对于GET/
,我有一个默认控制器Kafoso\TestApp\AppBundle\controller\DefaultController
:
namespace Kafoso\TestApp\AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
]);
}
}
该捆绑包确实添加到了app/AppKernel.php
(new Kafoso\TestApp\AppBundle()
)
我得到的错误是:
request.CRITICAL:Uncaught PHP异常Symfony\Component\Config\Exception\FileLoaderLoadException:“无法加载资源”@Kafoso/TestApp/AppBundle/Controller/。请确保“Kafoso”捆绑包已正确注册并加载到应用程序内核类中。如果已注册捆绑包,请确保捆绑包路径”@Kafoso/TestApp/AppBundle/Controller/“不是空的。”位于(…)/vhosts/TestApp/framework/server/vendor/symfony/symfony/src/symfony/Component/Config/Loader/Loader.php第73行
注意Symfony如何认为我的包的名称是“Kafoso”
奇怪的是,如果我在DefaultController
(类声明之外)中放入error\u log
或var\u dump
,我会看到输出。但出于某种原因,Symfony不会解析控制器的注释
这里不能使用名称空间吗?是否所有内容都必须放在AppBundle
下(或选择不同的名称)
如果是后者,这似乎是一种相当严格和不必要的强制执行
编辑
app/config/services.yml
:
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/Kafoso/TestApp/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Kafoso\TestApp\AppBundle\:
resource: '../../src/Kafoso/TestApp/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/Kafoso/TestApp/AppBundle/{Entity}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Kafoso\TestApp\AppBundle\Controller\:
resource: '../../src/Kafoso/TestApp/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# Kafoso\TestApp\AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
编辑
composer.json
:
{
"name": "kafoso/testapp",
"license": "proprietary",
"type": "project",
"authors": [
{
"name": "Kafoso",
"email": ""
}
],
"autoload": {
"psr-4": {
"Kafoso\\TestApp\\": "src/Kafoso/TestApp"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
"autoload-dev": {
"psr-4": {
"Kafoso\\TestApp\\AppBundle\\Tests\\Integration\\": "tests/integration",
"Kafoso\\TestApp\\AppBundle\\Tests\\Unit\\": "tests/unit"
},
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
"require": {
"php": "7.1.*",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
"incenteev/composer-parameter-handler": "^2.0",
"sensio/distribution-bundle": "^5.0.19",
"sensio/framework-extra-bundle": "^3.0.2",
"symfony/monolog-bundle": "^3.1.0",
"symfony/polyfill-apcu": "^1.0",
"symfony/swiftmailer-bundle": "^2.3.10",
"symfony/symfony": "3.3.*",
"twig/twig": "^1.0||^2.0",
"webmozart/glob": "^4.1"
},
"require-dev": {
"phpunit/phpunit": "^6.2",
"phpunit/dbunit": "^3.0",
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0"
},
"scripts": {
"symfony-scripts": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
],
"post-install-cmd": [
"@symfony-scripts"
],
"post-update-cmd": [
"@symfony-scripts"
]
},
"config": {
"sort-packages": true
},
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin",
"symfony-var-dir": "var",
"symfony-web-dir": "../../www",
"symfony-tests-dir": "tests",
"symfony-assets-install": "relative",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
},
"branch-alias": null
}
}
普通文件中的唯一更改是:
- 测试名称空间的测试规范,对这个问题没有影响
- 软件包的添加
webmozart/glob
- 更改
,这确实有效(symfony web dir
正在运行)www/app.php
PSR-4自动加载是
Kafoso\TestApp
,而不是Kafoso\TestApp\AppBundle
,但这应该不是必需的。您始终可以重命名on应用程序项目中的每个文件夹,只需更改配置文件属性即可
以下是一些提示:
- 如果存在旧的配置文件,请查看'app/config'中的配置文件
- 如果名称空间已正确更改,请检查所有“php文件”
- 检查Bundle`Resources/config中的`project config files``
- 完全删除“缓存文件夹”=>“bin/cache”,symfony将自动重新创建它
new Kafoso\TestApp\AppBundle\AppBundle()
对于注册,必须调用Bundle构造函数,该构造函数位于与Bundle文件夹同名的文件夹内的php文件中
。。。既然您添加了composer.json,为什么不使用:
"psr-0": { "": "src/" },
在自动加载块中,您始终可以重命名应用程序项目中的每个文件夹,只需更改配置文件属性即可 以下是一些提示:
- 如果存在旧的配置文件,请查看'app/config'中的配置文件
- 如果名称空间已正确更改,请检查所有“php文件”
- 检查Bundle`Resources/config中的`project config files``
- 完全删除“缓存文件夹”=>“bin/cache”,symfony将自动重新创建它
new Kafoso\TestApp\AppBundle\AppBundle()
对于注册,必须调用Bundle构造函数,该构造函数位于与Bundle文件夹同名的文件夹内的php文件中
。。。既然您添加了composer.json,为什么不使用:
"psr-0": { "": "src/" },
在自动加载块内部,问题在于symfony的自动加载 打开composer.json文件并编辑: 年金债券\年金债券 “自动加载”:{ “psr-4”:{ “AppBundle\”:“src/AppBundle”, “Kafoso\”:“src/Kafoso” }, “类映射”:[ “app/AppKernel.php”, “app/AppCache.php” ] }, 然后,在composer中运行下一个命令:
作曲家dumpautoload问题是symfony的自动加载 打开composer.json文件并编辑: 年金债券\年金债券 “自动加载”:{ “psr-4”:{ “AppBundle\”:“src/AppBundle”, “Kafoso\”:“src/Kafoso” }, “类映射”:[ “app/AppKernel.php”, “app/AppCache.php” ] }, 然后,在composer中运行下一个命令:
composer dumpautoload请在composer.json中替换
"psr-4": {
"[Bundles names]\\": "src/[same bundle]"
// Even if there is one or more bundles
},,
由此
},请在composer.json中替换
"psr-4": {
"[Bundles names]\\": "src/[same bundle]"
// Even if there is one or more bundles
},,
由此
},我刚刚在维护的一个项目中遇到了这个问题,问题是app/config/config.yml中的sensio_framework_额外捆绑包: 我改变了:
sensio_framework_extra:
router:
annotations: false
到
sensio\u框架\u额外:
路由器:
注释:true我在维护的一个项目中遇到了这个问题,问题是app/config/config.yml中的sensio_framework_额外捆绑包:
我改变了:
sensio_framework_extra:
router:
annotations: false
到
sensio\u框架\u额外:
路由器:
注释:是的,我做了你描述的一切,但我得到了同样的错误。有趣的是,将AppBundle
移动到同名文件夹不会影响错误消息。所以这个类永远不会被加载。我已多次清除缓存(var/cache
)。也许app/config/services.yml
会有所帮助。我把它加到了我原来的问题上,我试过了