Magento 2 - 'Area code not set:' 添加示例数据后

Magento 2 - 'Area code not set:' after adding sample data

所以我已经让 magento 2 正常工作 运行,我什至开始了我自己的主题,但我在安装 magento 之后尝试安装示例数据。它已经在终端中返回 'Area code not set:',下面是我的一些步骤 运行。

bin/magento sampledata:deploy    
composer update

哪个returns:

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing magento/module-catalog-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-bundle-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-widget-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-customer-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/sample-data-media (100.0.3)
    Downloading: 100%         

  - Installing magento/module-theme-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-cms-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-catalog-rule-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-sales-rule-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-review-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-tax-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-grouped-product-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-downloadable-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-msrp-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-sales-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-product-links-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-configurable-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-wishlist-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-swatches-sample-data (100.0.3)
    Downloading: 100%         

  - Installing magento/module-offline-shipping-sample-data (100.0.3)
    Downloading: 100%         

Writing lock file
Generating autoload files

然后我运行:

bin/magento setup:upgrade

我在下面的屏幕截图中看到了以下错误,我似乎在网上找不到其他人有同样的错误。

  [Magento\Framework\Exception\SessionException]                       
  Area code not set: Area code must be set before starting a session.  

  [Magento\Framework\Exception\LocalizedException]  
  Area code is not set    

现在我的法师安装无法正常工作 - 错误 returns:

请从 Magento 根目录升级您的数据库:运行 "bin/magento setup:upgrade"。

我做不到,因为它 returns 和错误,任何帮助都会很棒。

我遇到的关于这个特定错误的唯一问题是人们从以前版本的 Magento 2 beta 更新。您可以在 Magento 2 回购问题跟踪器下的 Github 上阅读有关此的所有详细信息:Area code is not set while setup:upgrade #1529.

综上所述,开发人员将升级说明更改为以下内容:

Basically, you start with uninstall but no do NOT run setup:upgrade. Instead, update the code, composer update, install again.

他们参考 this article on Magento 对其进行了更多解释。

现在我不知道这是否也能解决您的错误,但这似乎是同一个问题。此外,尽管 Magento 2 允许在安装后安装示例数据,但众所周知,这比从头开始安装要麻烦得多。以下是 Mageto 提供的一些资源,您可能需要查看这些资源以确保一切正确:

还有一种预感...(查看编辑)...尝试改回原来的 Luma 主题,看看是否有所不同。

编辑
凭直觉,我的意思是我遇到了一个与此类似的大问题,我从来没有足够关心检查它导致它的原因,但是切换回默认主题并更新解决了我所有的问题。然后我又切换回我自己的主题,从那以后就再也没有问题了。

有同样的问题并且能够解决它。

首先尝试运行执行此命令:

php bin/magento sampledata:reset

然后再运行:

php bin/magento setup:upgrade

如果您遇到内存耗尽错误(我遇到过),请尝试在您的 setup:upgrade 命令中添加 -dmemory_limit=6G

php -dmemory_limit=6G bin/magento setup:upgrade

我在安装Magento 2.0.7 后遇到同样的问题,然后尝试安装示例数据。

Module 'Magento_ConfigurableSampleData':  [Magento\Framework\Exception\SessionException]  Area code not set: Area code must be set before starting a session.

我也试过重置(phpbin/magentosampledata:reset),但是又出现错误:

  [Zend_Db_Statement_Exception]
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module' cannot be null, query was: INSERT INTO `setup_module` (`modul
  e`, `data_version`) VALUES (?, ?)

因为我的设置是基于 CLI (php bin/magento setupinstall --...),所以我不知道为什么。直到它在网站前面再次设置。在设置 php 检查过程中,存在 php 值错误:

Your PHP Version is 5.6.18, but always_populate_raw_post_data = 0. $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will be removed in PHP 7.0. This will stop the installer from running. Please open your php.ini file and set always_populate_raw_post_data to -1. If you need more help please call your hosting provider.

因此,在 php.ini 或 .htaccess 中设置 always_populate_raw_post_data = -1 后。 (如果需要,重新启动 php 或 php-fpm)。然后,安装示例数据并运行 好。

希望这些步骤对某些人有所帮助。

首先尝试运行执行此命令:

php bin/magento app:config:import

然后清除缓存和运行setup:upgrade

我有同样的问题并且能够解决

仅对 magento (2.4.1) 尝试此命令

php -dmemory_limit=5G bin/magento cache:flush
php -dmemory_limit=5G bin/magento sampledata:reset
php -dmemory_limit=5G bin/magento setup:upgrade

为 magento (2.4.2+) 尝试此命令 在任何扩展中搜索 setAreaCode() 方法的用法,并检查从何处调用它们。

替换代码

protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->appState->setAreaCode('frontend');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
try{
    $output->setDecorated(true);
    $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
       // do nothing
   }
}

交替使用核心\Magento\Framework\App\State::emulateAreaCode方法临时设置区号:

$self = $this;
   try {
       $this->appState->emulateAreaCode('frontend', function () use ($self) {
           // operations
       });
   } catch (\Exception $exception) {
       // handle exception
   }

我在 Magento 2.4.2 上安装示例数据并再次使用 Composer 卸载几个选定的示例数据模块后遇到了这个问题。为了解决这个问题,我手动更改了 app\etc\config.php,方法是为示例数据不需要的那些扩展设置 0 而不是 1(我尝试使用 CLI 命令删除的扩展)。