不考虑 symfony 4 的变化

Posted

技术标签:

【中文标题】不考虑 symfony 4 的变化【英文标题】:symfony 4 changes are not taken into account 【发布时间】:2019-03-24 16:47:39 【问题描述】:

我已经将一个 symfony 4 应用程序从生产服务器迁移到我用于测试的服务器上,两个服务器都在 wampserver 上。该应用程序在测试服务器上运行良好,但问题是当我进行更改时,它们没有被考虑在内。每次我更改树枝文件时,更改都不会显示。我认为它正在使用 prod 和 dev 变量,但我是 Symfony 的新手,我不确定它是如何工作的。显然现在服务器处于 prod 模式,我想将其更改为 dev 模式。 vhosts 文件的代码是

# Virtual Hosts
#
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  SetEnv APP_ENV prod
  SetEnv APP_SECRET b33c77e602bc1bafb66f673c1e31aca3
  SetEnv DATABASE_URL mysql://root@127.0.0.1:3306/gpac
  DocumentRoot "$INSTALL_DIR/www/gpac/public"
  <Directory "$INSTALL_DIR/www/gpac/public/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

public/index.php

<?php

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

require __DIR__.'/../vendor/autoload.php';

// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) 
    if (!class_exists(Dotenv::class)) 
        throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
    
    (new Dotenv())->load(__DIR__.'/../.env');


$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));

if ($debug) 
    umask(0000);

    Debug::enable();


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


if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) 
    Request::setTrustedHosts(explode(',', $trustedHosts));


$kernel = new Kernel($env, $debug);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

当我运行 php bin/console cache:clear --env=prod 时会报错,因为 .env 文件不存在 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:
    locale: 'fr'
    version: 1.0.0
    default_alert:
        council_meeting: 7 # days
        project_step: 7 # days
        resource_report: 1 # years
        resource_contract: 1 # month
        resource_capability: 1 # month

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
    App\:
        resource: '../src/*'
        exclude: '../src/Entity,Migrations,Tests,Kernel.php'

    # 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

    activity:
        class: App\Service\ActivityLogger
        public: true
        arguments: ['@doctrine.orm.entity_manager', '@security.token_storage', '@request_stack']

    process:
        class: App\Service\ProcessProvider
        public: true
        arguments: ['%kernel.project_dir%']

    App\Listener\LoginListener:
        arguments: ['@activity']
        tags:
            -  name: kernel.event_subscriber 

    dpolac.twig_lambda.extension:
        class: DPolac\TwigLambda\LambdaExtension
        tags: [  name: twig.extension  ]

    # ---------------------
    # Sonata Admin Services
    # ---------------------

    app.admin.user:
        public: true
        class: App\Admin\UserAdmin
        arguments: [~, App\Entity\User, App\Controller\UserAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, group: admin, label: class.User 

    app.admin.user_function:
        public: true
        class: App\Admin\UserFunctionAdmin
        arguments: [~, App\Entity\UserFunction, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: admin, label: class.UserFunction 

    app.admin.skill_matrix:
        class: App\Admin\SkillMatrixAdmin
        arguments: [~, App\Entity\SkillMatrix, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.SkillMatrix 

    app.admin.skill_matrix_category:
        class: App\Admin\SkillMatrixCategoryAdmin
        arguments: [~, App\Entity\SkillMatrixCategory, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: skill, label: class.SkillMatrixCategory 
        calls:
            - [ setTemplate, [list, 'CRUD/Skill/list.html.twig']]

    app.admin.client:
        class: App\Admin\ClientAdmin
        arguments: [~, App\Entity\Client, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: project, label: class.Client 
        calls:
            - [addChild, ['@app.admin.client_site']]

    app.admin.client_site:
        class: App\Admin\ClientSiteAdmin
        arguments: [~, App\Entity\ClientSite, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ClientSite

    app.admin.client_numbers:
        class: App\Admin\ClientNumbersAdmin
        arguments: [~, App\Entity\ClientNumbers, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ClientNumbers

    app.admin.resource:
        class: App\Admin\ResourceAdmin
        arguments: [~, App\Entity\Resource, SonataAdminBundle:CRUD, '@doctrine.orm.entity_manager']
        tags:
            -  name: sonata.admin, manager_type: orm, group: rc, label: class.Resource 
        public: true
        calls:
            - [ setTemplate, [show, 'CRUD/Resource/show.html.twig']]
            - [addChild, ['@app.admin.resource_capability']]
            - [addChild, ['@app.admin.resource_contract']]
            - [addChild, ['@app.admin.resource_education']]
            - [addChild, ['@app.admin.resource_experience']]
            - [addChild, ['@app.admin.resource_lang']]
            - [addChild, ['@app.admin.resource_report']]

    app.admin.council:
        public: true
        class: App\Admin\CouncilAdmin
        arguments: [~, App\Entity\Council, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: council, label: class.Council 
        calls:
            - [ setTemplate, [list, 'CRUD/Council/list.html.twig']]

    app.admin.council_meeting:
        class: App\Admin\CouncilMeetingAdmin
        arguments: [~, App\Entity\CouncilMeeting, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: council, label: class.CouncilMeeting 
        public: true

    app.admin.resource_capability:
        class: App\Admin\ResourceCapabilityAdmin
        arguments: [~, App\Entity\ResourceCapability, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm , label: class.ResourceCapability
        public: true

    app.admin.resource_contract:
        class: App\Admin\ResourceContractAdmin
        arguments: [~, App\Entity\ResourceContract, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ResourceContract
        public: true

    app.admin.resource_education:
        class: App\Admin\ResourceEducationAdmin
        arguments: [~, App\Entity\ResourceEducation, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm , label: class.ResourceEducation
        public: true

    app.admin.resource_experience:
        class: App\Admin\ResourceExperienceAdmin
        arguments: [~, App\Entity\ResourceExperience, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm , label: class.ResourceExperience
        public: true

    app.admin.resource_lang:
        class: App\Admin\ResourceLangAdmin
        arguments: [~, App\Entity\ResourceLang, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ResourceLang
        public: true

    app.admin.project:
        class: App\Admin\ProjectAdmin
        arguments: [~, App\Entity\Project, App\Controller\ProjectAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, group: project, label: class.Project 
        public: true
        calls:
            - [setTemplate, ['list', 'CRUD/Project\list.html.twig']]
            - [setTemplate, ['show', 'CRUD/Project\show.html.twig']]
            - [setTemplate, ['edit', 'CRUD/Project\edit.html.twig']]
            - [addChild, ['@app.admin.project_eval']]
            - [addChild, ['@app.admin.project_status']]
            - [addChild, ['@app.admin.project_alert']]

    app.admin.project_status:
        class: App\Admin\ProjectStatusAdmin
        arguments: [~, App\Entity\ProjectStatus, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ProjectStatus 
        public: true

    app.admin.project_eval:
        class: App\Admin\ProjectEvalAdmin
        arguments: [~, App\Entity\ProjectEval, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ProjectEval 
        public: true
        calls:
            - [addChild, ['@app.admin.project_step']]
            - [addChild, ['@app.admin.project_staff']]

    app.admin.project_staff:
        class: App\Admin\ProjectStaffAdmin
        arguments: [~, App\Entity\ProjectStaff, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ProjectStaff 
        public: true

    app.admin.project_step:
        class: App\Admin\ProjectStepAdmin
        arguments: [~, App\Entity\ProjectStep, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ProjectStep 
        public: true

    app.admin.activity:
        class: App\Admin\ActivityAdmin
        arguments: [~, App\Entity\Activity, App\Controller\ActivityAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, group: admin, label: class.Activity 
        public: true

    app.admin.project_alert:
        class: App\Admin\ProjectAlertAdmin
        arguments: [~, App\Entity\ProjectAlert, App\Controller\ProjectAlertAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ProjectAlert 
        public: true

    app.admin.report_qual_theo:
        class: App\Admin\ReportQualTheoAdmin
        arguments: [~, App\Entity\ReportQualTheo, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: frc, label: class.ReportQualTheo 
        public: true

    app.admin.report_qual_prat:
        class: App\Admin\ReportQualPratAdmin
        arguments: [~, App\Entity\ReportQualPrat, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: frc, label: class.ReportQualPrat 
        public: true

    app.admin.resource_report:
        class: App\Admin\ResourceReportAdmin
        arguments: [~, App\Entity\ResourceReport, App\Controller\ResourceReportAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, group: frc, label: class.ResourceReport 
        public: true
        calls:
            - [addChild, ['@app.admin.resource_report_education']]
            - [addChild, ['@app.admin.resource_report_forum']]
            - [addChild, ['@app.admin.resource_report_project']]

    app.admin.resource_report_education:
        class: App\Admin\ResourceReportEducationAdmin
        arguments: [~, App\Entity\ResourceReportEducation, App\Controller\ResourceReportEducationAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ResourceReportEducation 
        public: true

    app.admin.resource_report_forum:
        class: App\Admin\ResourceReportForumAdmin
        arguments: [~, App\Entity\ResourceReportForum, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ResourceReportForum 
        public: true

    app.admin.resource_report_project:
        class: App\Admin\ResourceReportProjectAdmin
        arguments: [~, App\Entity\ResourceReportProject, App\Controller\ResourceReportProjectAdminController]
        tags:
            -  name: sonata.admin, manager_type: orm, label: class.ResourceReportProject 
        public: true
        calls:
          - ['setTemplate', ['list', 'CRUD/ReportResourceProject\list.html.twig']]

    app.admin.report_council:
        class: App\Admin\ReportCouncilAdmin
        arguments: [~, App\Entity\ReportCouncil, SonataAdminBundle:CRUD]
        tags:
            -  name: sonata.admin, manager_type: orm, group: frc, label: class.ReportCouncil 
        public: true

【问题讨论】:

【参考方案1】:

对于 symfony4 控制台.htaccesswebserver 中给出的环境变量无效。所以你需要在命令中提供环境变量。

您应该运行以下命令来清除生产中的缓存

APP_ENV=prod php bin/console cache:clear

如果你是从 windows cmd 运行命令,那么你可能需要像这样运行:

set APP_ENV=prod php bin/console cache:clear

【讨论】:

在windows上运行这个命令会报错:APP_ENV环境变量没有定义。 我从不同的安装导入了一个 .env 文件,并在 windows 上执行了命令。它没有显示任何消息,但没有显示错误消息。问题是更改仍然没有显示在网站上 现在我对树枝文件所做的更改正确显示。但问题是页面加载非常缓慢(加载需要几分钟)有什么建议吗? 如果第一次加载很慢。你可以运行cache:warmup 命令。如果它在本地和服务器上总是很慢,你应该检查分析器以了解它在哪里花费时间。【参考方案2】:

您可以尝试在 VirtualHost 文件中将“SetEnv APP_ENV prod”更改为“SetEnv APP_ENV dev”。

# Virtual Hosts
#
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  SetEnv APP_ENV dev #<-------------------------------------- here
  SetEnv APP_SECRET b33c77e602bc1bafb66f673c1e31aca3
  SetEnv DATABASE_URL mysql://root@127.0.0.1:3306/gpac
  DocumentRoot "$INSTALL_DIR/www/gpac/public"
  <Directory "$INSTALL_DIR/www/gpac/public/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

【讨论】:

我试过了,它在本地主机上显示一个错误,因为页面加载超过了 120 秒的执行时间 @kadri.r 您能否添加服务配置文件的内容(在 config/services.yml 和 config/packages/dev/*.yml 中)? 我已将 config/services.yaml 添加到问题中,但 config/packages/dev/*.yml 中有很多文件我应该添加哪些文件(那里有 3 个文件夹,产品、开发和测试)

以上是关于不考虑 symfony 4 的变化的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 4 不自动装配动态路由控制器

Symfony 属性路由未考虑在内

动态变化的形式是 Symfony 5

Symfony 4.4 - 自定义错误模板不起作用

Symfony 4 - UniqueEntity 约束不显示消息错误

Symfony 4 / doctrine插入初始数据库数据