【问题标题】:Attempted to load class "WebpackEncoreBundle" from namespace "Symfony\WebpackEncoreBundle". Did you forget a "use" statement for another namespace?尝试从命名空间“Symfony\WebpackEncoreBundle”加载类“WebpackEncoreBundle”。您是否忘记了另一个名称空间的“使用”语句?
【发布时间】:2019-11-18 21:29:35
【问题描述】:

我有一个奇怪的问题。 我使用 Symfony Flex,我在现有项目上工作。我尝试使用以下命令安装 webpack-encore-bundle:

composer require symfony/webpack-encore-bundle

然后我明白了:

./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.2.*"
Package operations: 1 install, 0 updates, 0 removals
  - Installing symfony/webpack-encore-bundle (v1.6.2): Loading from cache
Package symfony/webpack-encore-pack is abandoned, you should avoid using it. Use symfony/webpack-encore-bundle instead.
Writing lock file
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Symfony operations: 1 recipe (8e557491367cda509fceebb187833635)
  - Configuring symfony/webpack-encore-bundle (>=1.0): From github.com/symfony/recipes:master
Executing script cache:clear [OK]
Executing script assets:install --symlink --relative public [OK]

Some files may have been created or updated to configure your new packages.
Please review, edit and commit them: these files are yours.

然后我运行:

yarn install

我明白了:

    yarn install v1.16.0
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.9: The platform "linux" is incompatible with this module.
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > sass-loader@7.1.0" has unmet peer dependency "webpack@^3.0.0 || ^4.0.0".
[4/4] Building fresh packages...
success Saved lockfile.
Done in 2.39s.

一切似乎都很好。现在我刷新项目,我得到了这个:

ClassNotFoundException

Attempted to load class "WebpackEncoreBundle" from namespace "Symfony\WebpackEncoreBundle".
Did you forget a "use" statement for another namespace?

这就是我在 bundles.php 中的内容:

Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],

在 composer.json 我有:

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

我跑:

yarn encore dev

它编译成功,但我有同样的错误。

我也试过这个命令:

composer remove symfony/webpack-encore-bundle

那我再试一次

composer require symfony/webpack-encore-bundle

然后我们遇到了同样的错误,这个循环永远不会结束。

【问题讨论】:

标签: php symfony webpack webpack-encore


【解决方案1】:

我有这个错误,我只是通过安装webpack-encore-bundle 来修复它:

composer require symfony/webpack-encore-bundle

就我而言,我必须在 webpack-encore-bundle 之前安装 doctrine-orm-admin-bundle

composer require sonata-project/doctrine-orm-admin-bundle

【讨论】:

    【解决方案2】:

    你是否使用 Vagrant(例如通过 Laravel Homestead)或任何其他类似的工具? 我发现在这种情况下,它是一个没有同步到现有框的文件夹。

    可能遇到的问题:

    • 在项目已经启动并在盒子中运行之后,已经安装了 Webpack 依赖项
    • 供应商文件夹已从同步过程中排除(似乎也导致无法通过重新配置更新)

    这似乎有点离题,但我自己在这个问题上花了大约 2 天的时间,我还没有为这个特定的问题组合找到任何答案。 这就是我与您深入分享的原因。


    如何解决

    让我们假设代码在一个盒子中运行并同步到它,这里是最终检查和修复这个特定场景和依赖关系的方法。

    1。修复

    1. 关闭运行箱:vagrant halt
    2. 检查 Composer 依赖项是否是最新的,或者这样做:composer installcomposer update
    3. 检查 Homestead.yaml 是否不排除要同步的 vendor 文件夹 (?)
    4. 为盒子重新运行配置:vagrant up --provision

    2。测试

    1. SSH 进入运行框
    2. 导航到您的源代码文件夹
    3. 检查路径vendor/symfony/webpack-encore-bundle是否存在
    4. 打开项目 URL 并检查它现在是否正常工作

    旁注

    流浪者+宅基地?

    您可以为此使用任何其他技术堆栈 - 不同步背后的基本问题应该在任何地方都相似。 例如,在 Docker 中,这可能是一个缓存问题,具体取决于您的设置。

    Homestead.yaml

    假设您将 Laravel Homestead 与 Vagrant 一起使用并进行 'rsync' 文件夹更新,那么您可能想要包含 'vendor' 文件夹 (请注意,这会消耗同步性能)。此设置适用于 Mac,但任何其他同步方法都应该类似。

    我不确定 100% 是否需要重新配置,但就我而言,它似乎可以解决问题。

    ...
    
    folders:
        - map: ...
          to: ...
          type: "rsync" # nfs
          options:
              rsync__auto: true
              rsync__args: ["--verbose", "--archive", "--delete", "-zz"]
              # rsync__exclude: [".git", "node_modules", "vendor"]
              rsync__exclude: [".git", "node_modules"] # Include vendor for any updates without rebuilding the box
    

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 2015-11-07
      • 2020-05-24
      • 2019-10-09
      • 2016-11-07
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多