【问题标题】:Symfony 4 do not build in Production: Attempted to load class "WebProfilerBundle" from namespaceSymfony 4 不在生产环境中构建:尝试从命名空间加载类“WebProfilerBundle”
【发布时间】:2020-01-02 17:18:45
【问题描述】:

Symfony 4 确实在 Dev 模式下构建,但在产生错误时不要在 Production 模式下:

尝试从命名空间加载类“WebProfilerBundle”。

WebProfilerBundle 确实已安装,但只能在开发环境中使用,而不应在生产版本中使用。

我在 Fedora30 上,不涉及容器化。

  • php -v => PHP 7.3.8 (cli)(构建时间:2019 年 7 月 30 日 09:26:16)(NTS)
  • npm -v => 6.9.0
  • yarn -v => 1.17.3
  • composer.phar -V => Composer 版本 1.9.0 2019-08-02 20:55:32
  • symfony -V => Symfony CLI 版本 v4.6.4(2019 年 8 月 13 日星期二 16:14:53 CEST)
  • 弹性

活动文件版本控制让我猜测使用了生产构建链(不存在开发构建):

$ ls -l public/build
168 -rw-rw-r-- 1 rob rob 169693 31. Aug 15:39 0.376e4878.js
184 -rw-rw-r-- 1 rob rob 185359 31. Aug 15:39 0.6ea45216.css
  4 -rw-rw-r-- 1 rob rob    404 31. Aug 15:39 app.00b96fcb.js
  4 -rw-rw-r-- 1 rob rob    213 31. Aug 15:39 app.f7e93431.css
  4 -rw-rw-r-- 1 rob rob    261 31. Aug 15:39 entrypoints.json
  4 drwxrwxr-x 2 rob rob   4096 31. Aug 15:39 fonts
  4 drwxrwxr-x 2 rob rob   4096 31. Aug 15:39 images
  4 -rw-rw-r-- 1 rob rob   1246 31. Aug 15:39 manifest.json
  4 -rw-rw-r-- 1 rob rob   1505 31. Aug 15:39 runtime.3c075ebb.js

bundles.php

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],

    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],

    Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
    Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
    Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
];

composer.json

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "sensio/framework-extra-bundle": "^5.4",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "4.3.*",
        "symfony/console": "4.3.*",
        "symfony/dotenv": "4.3.*",
        "symfony/expression-language": "4.3.*",
        "symfony/flex": "^1.4",
        "symfony/form": "4.3.*",
        "symfony/framework-bundle": "4.3.*",
        "symfony/http-client": "4.3.*",
        "symfony/intl": "4.3.*",
        "symfony/monolog-bundle": "^3.4",
        "symfony/orm-pack": "*",
        "symfony/process": "4.3.*",
        "symfony/security-bundle": "4.3.*",
        "symfony/serializer-pack": "*",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/translation": "4.3.*",
        "symfony/twig-bundle": "4.3.*",
        "symfony/validator": "4.3.*",
        "symfony/web-link": "4.3.*",
        "symfony/webpack-encore-bundle": "^1.6",
        "symfony/yaml": "4.3.*"
    },
    "require-dev": {
        "symfony/debug-pack": "*",
        "symfony/maker-bundle": "^1.0",
        "symfony/profiler-pack": "*",
        "symfony/test-pack": "*",
        "symfony/web-server-bundle": "4.3.*"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.3.*"
        }
    }
}

生产版本是用以下方式构建的:

  • 做$cp .env.local.prod .env.local这包含..
    • 同.env
    • 除此行APP_ENV=prod
    • 除了缺少的 APP_SECRET 行
  • 做$rm -rf var/cache; rm -rf vendor
  • 做$../composer.phar install --no-dev --prefer-dist --optimize-autoloader --no-interaction
  • 做$yarn build
  • 在完成构建时执行 $ rm .env.local

但这似乎不起作用,它会从上面产生错误。

开发构建完成:

  • 做$rm .env.local just use .env with APP_SECRET and APP_ENV=dev
  • 做$rm -rf var/cache; rm -rf vendor
  • 做$../composer.phar install
  • 做$yarn encore dev

网站的错误输出

Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle". Did you forget a "use" statement for another namespace?
(1/1) ClassNotFoundException
Attempted to load class "WebProfilerBundle" from namespace "Symfony\Bundle\WebProfilerBundle".
Did you forget a "use" statement for another namespace?

in Kernel.php line 23
at Kernel->registerBundles()
in Kernel.php line 424
at Kernel->initializeBundles()
in Kernel.php line 130
at Kernel->boot()
in Kernel.php line 193
at Kernel->handle(object(Request))
in index.php line 25

构建过程

Loading composer repositories with package information
Installing dependencies from lock file
Package operations: 92 installs, 0 updates, 0 removals
  - Installing ocramius/package-versions (1.5.1): Loading from cache
  - Installing symfony/flex (v1.4.5): Loading from cache
  - Installing doctrine/lexer (1.1.0): Loading from cache
  - Installing doctrine/annotations (v1.7.0): Loading from cache
  - Installing doctrine/reflection (v1.0.0): Loading from cache
  - Installing doctrine/event-manager (v1.0.0): Loading from cache
  - Installing doctrine/collections (v1.6.2): Loading from cache
  - Installing doctrine/cache (v1.8.0): Loading from cache
  - Installing doctrine/persistence (1.1.1): Loading from cache
  - Installing doctrine/inflector (v1.3.0): Loading from cache
  - Installing doctrine/common (v2.10.0): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing symfony/service-contracts (v1.1.5): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.12.0): Loading from cache
  - Installing symfony/doctrine-bridge (v4.3.3): Loading from cache
  - Installing doctrine/doctrine-cache-bundle (1.3.5): Loading from cache
  - Installing doctrine/instantiator (1.2.0): Loading from cache
  - Installing symfony/stopwatch (v4.3.3): Loading from cache
  - Installing symfony/polyfill-php73 (v1.12.0): Loading from cache
  - Installing symfony/console (v4.3.3): Loading from cache
  - Installing zendframework/zend-eventmanager (3.2.1): Loading from cache
  - Installing zendframework/zend-code (3.3.1): Loading from cache
  - Installing ocramius/proxy-manager (2.2.3): Loading from cache
  - Installing doctrine/dbal (v2.9.2): Loading from cache
  - Installing doctrine/migrations (2.1.1): Loading from cache
  - Installing egulias/email-validator (2.1.11): Loading from cache
  - Installing jdorn/sql-formatter (v1.2.17): Loading from cache
  - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
  - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
  - Installing symfony/polyfill-php72 (v1.12.0): Loading from cache
  - Installing symfony/polyfill-intl-idn (v1.12.0): Loading from cache
  - Installing symfony/mime (v4.3.3): Loading from cache
  - Installing symfony/http-foundation (v4.3.3): Loading from cache
  - Installing symfony/event-dispatcher-contracts (v1.1.5): Loading from cache
  - Installing symfony/event-dispatcher (v4.3.3): Loading from cache
  - Installing psr/log (1.1.0): Loading from cache
  - Installing symfony/debug (v4.3.3): Loading from cache
  - Installing symfony/http-kernel (v4.3.3): Loading from cache
  - Installing symfony/routing (v4.3.3): Loading from cache
  - Installing symfony/finder (v4.3.3): Loading from cache
  - Installing symfony/filesystem (v4.3.3): Loading from cache
  - Installing symfony/dependency-injection (v4.3.3): Loading from cache
  - Installing symfony/config (v4.3.3): Loading from cache
  - Installing symfony/var-exporter (v4.3.3): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing symfony/cache-contracts (v1.1.5): Loading from cache
  - Installing symfony/cache (v4.3.3): Loading from cache
  - Installing symfony/framework-bundle (v4.3.3): Loading from cache
  - Installing sensio/framework-extra-bundle (v5.4.1): Loading from cache
  - Installing symfony/apache-pack (v1.0.1): Loading from cache
  - Installing symfony/dotenv (v4.3.3): Loading from cache
  - Installing symfony/expression-language (v4.3.3): Loading from cache
  - Installing symfony/inflector (v4.3.3): Loading from cache
  - Installing symfony/property-access (v4.3.3): Loading from cache
  - Installing symfony/options-resolver (v4.3.3): Loading from cache
  - Installing symfony/intl (v4.3.3): Loading from cache
  - Installing symfony/polyfill-intl-icu (v1.12.0): Loading from cache
  - Installing symfony/form (v4.3.3): Loading from cache
  - Installing symfony/http-client-contracts (v1.1.5): Loading from cache
  - Installing symfony/http-client (v4.3.3): Loading from cache
  - Installing monolog/monolog (1.24.0): Loading from cache
  - Installing symfony/monolog-bridge (v4.3.3): Loading from cache
  - Installing symfony/monolog-bundle (v3.4.0): Loading from cache
  - Installing doctrine/orm (v2.6.3): Loading from cache
  - Installing doctrine/doctrine-bundle (1.11.2): Loading from cache
  - Installing doctrine/doctrine-migrations-bundle (v2.0.0): Loading from cache
  - Installing symfony/orm-pack (v1.0.6): Loading from cache
  - Installing symfony/process (v4.3.3): Loading from cache
  - Installing symfony/security-core (v4.3.3): Loading from cache
  - Installing symfony/security-http (v4.3.3): Loading from cache
  - Installing symfony/security-guard (v4.3.3): Loading from cache
  - Installing symfony/security-csrf (v4.3.3): Loading from cache
  - Installing symfony/security-bundle (v4.3.3): Loading from cache
  - Installing symfony/serializer (v4.3.3): Loading from cache
  - Installing symfony/property-info (v4.3.3): Loading from cache
  - Installing webmozart/assert (1.5.0): Loading from cache
  - Installing phpdocumentor/reflection-docblock (4.3.1): Loading from cache
  - Installing symfony/serializer-pack (v1.0.2): Loading from cache
  - Installing swiftmailer/swiftmailer (v6.2.1): Loading from cache
  - Installing symfony/swiftmailer-bundle (v3.2.8): Loading from cache
  - Installing symfony/translation-contracts (v1.1.5): Loading from cache
  - Installing symfony/translation (v4.3.3): Loading from cache
  - Installing twig/twig (v2.11.3): Loading from cache
  - Installing symfony/twig-bridge (v4.3.3): Loading from cache
  - Installing symfony/twig-bundle (v4.3.3): Loading from cache
  - Installing symfony/validator (v4.3.3): Loading from cache
  - Installing psr/link (1.0.0): Loading from cache
  - Installing fig/link-util (1.0.0): Loading from cache
  - Installing symfony/web-link (v4.3.3): Loading from cache
  - Installing symfony/asset (v4.3.3): Loading from cache
  - Installing symfony/webpack-encore-bundle (v1.6.2): Loading from cache
  - Installing symfony/yaml (v4.3.3): Loading from cache
Generating optimized autoload files
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [OK]
Executing script assets:install public [OK]

yarn run v1.17.3
$ encore production --progress
Running webpack ...

98% after emitting SizeLimitsPlugin DONE  Compiled successfully in 5450ms                                                                                                                                                            1:09:20 PM

 I  18 files written to public/build
Entrypoint app [big] = runtime.3c075ebb.js 0.6ea45216.css 0.376e4878.js app.f7e93431.css app.00b96fcb.js
Entrypoint _tmp_copy = runtime.3c075ebb.js
Done in 7.52s.

我想知道的是,也许它的帮助,在 public/index.php 中也是调试“命令”:

use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;

require dirname(__DIR__).'/config/bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
    umask(0000);

    Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
    Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
    Request::setTrustedHosts([$trustedHosts]);
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

【问题讨论】:

  • 检查您的config/bundles.php 以确保它仅对devtest 环境有效,而不是all
  • 可能不应该删除 env.local。听起来那是设置 APP_ENV=prod 的地方。在真实的生产环境中,您将拥有一个覆盖所有本地 .env 文件的真实环境变量集。看起来你没有任何设置。默认 .env 文件中有一个指向配置文档最佳实践的链接。
  • @msg 抱歉,我忘了提供 bundles.php。用那个更新了帖子。
  • 配置看起来不错,所以我使用 Cerad,APP_ENV 默认为 dev,因为其他任何地方都没有设置。
  • @row 但是在构建之后,如果没有在其他任何地方设置环境(即dev),那么您将 remove .env.local 回到默认值。即使容器是在生产环境中编译的,bootstrap 也会尝试加载当前的 env 依赖项,但它们并不存在。构建和运行时必须在同一个环境中配置。

标签: php symfony namespaces production


【解决方案1】:

非常感谢@msg 解决了这个问题。我的期望是环境变量只对创建构建很重要,并且它们在某种程度上是静态的。但正如here 已经解释的那样,环境变量在运行时也很重要。所以因为我在构建后删除了我的 .env.local,所以运行时环境立即改回了 dev。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多