Symfony:未正确加载Namespaced包

时间:2017-07-20 16:33:45

标签: php symfony configuration

我有一个新安装的(通过作曲家)Symfony应用程序。我已经把事情转移了一下,似乎这导致Symfony的控制器注释驱动程序崩溃了。最值得注意的是,我正在使用命名空间。

我的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

Kafoso\TestApp\AppBundle\Controller\DefaultController我有一个默认控制器GET /

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.phpnew Kafoso\TestApp\AppBundle())。

我得到的错误:

  

request.CRITICAL:未捕获的PHP异常Symfony \ Component \ Config \ Exception \ FileLoaderLoadException:“无法加载资源”@ Kafoso / TestApp / AppBundle / Controller /“。确保”Kafoso“软件包已正确注册并加载到应用程序内核类。如果已注册该包,请确保包路径“@ Kafoso / TestApp / AppBundle / Controller /”不为空。“ at(...)/ vhosts / testapp / framework / server / vendor / symfony / symfony / src / Symfony / Component / Config / Loader / Loader.php第73行

请注意Symfony如何认为我的软件包的名称是“Kafoso”。

奇怪的是,如果我在error_log(在类声明之外)放置var_dumpDefaultController,我会看到输出。但由于某种原因,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
    }
}

vanilla文件的唯一变化是:

  • 测试命名空间的规范,对此问题没有影响。
  • 添加了包webmozart/glob
  • symfony-web-dir的更改,确实有效(www/app.php正在运行)。

PSR-4自动加载是Kafoso\TestApp,而不是Kafoso\TestApp\AppBundle,但这不是必需的。

5 个答案:

答案 0 :(得分:1)

您始终可以重命名应用程序项目中的每个文件夹,只需更改配置文件

以下是一些提示:

  • 如果有旧的
  • ,请查看`app / config`中的配置文件
  • 如果名称空间被正确更改,请检查所有`php-files`
  • 检查Bundle`Eources / config`
  • 中的`project config-files`
  • 删除`cache-folder` => `bin / cache`完全,symfony会自动重新创建它

真正的问题是看到你的问题,......错误信息是正确的,非常清楚:

new Kafoso\TestApp\AppBundle\AppBundle()

注册必须调用Bundle构造函数,这位于文件夹内的php文件中,该文件夹与bundle-folder同名

...既然您添加了composer.json,为什么不使用:

"psr-0": { "": "src/" },

在自动加载块

答案 1 :(得分:1)

问题是symfony的自动加载。

打开你的composer.json文件并编辑:

AnnuaireBundle \ AnnuaireBundle

" autoload":{     " psr-4":{         " AppBundle \":" src / AppBundle",         " Kafoso \":" src / Kafoso"     },     " classmap":[         "应用程序/ AppKernel.php&#34 ;,         "应用程序/ AppCache.php"     ] },

然后,在你的作曲家中运行下一个命令:

composer dumpautoload

答案 2 :(得分:0)

请在composer.json中替换

"psr-4": {
"[Bundles names]\\": "src/[same bundle]"
// Even if there is one or more bundles

}, 通过这个

"psr-4": {
"": "src/"

},

答案 3 :(得分:0)

我在我维护的项目中遇到了这个问题,问题是app / config / config.yml中的sensio_framework_extra捆绑包:

我更改了:

sensio_framework_extra:
        router:
            annotations: false 

收件人

sensio_framework_extra:
       router:
            annotations: true      <-----

我所有的路线都使用注释,所以这很奇怪。更改它,它开始工作!

答案 4 :(得分:0)

file: config.yml 
fos_rest:
  routing_loader:
"If **enable:true** / Just change this for **false**. so if this is false, change this for true, and restart the project, i'm thinks this is a bug when we're upgrading to a new version!!, and i fixed in the way that I'm mentioning to you
"
    **enabled: false**