2

First of all, I have to tell you that I have spent a lot of time researching and I have done all the solutions I have came across but I'm still getting this error when I run setup:di:compile

The full error is as follows:

Compilation was started.
Interception cache generation... 7/7 [============================] 100% 3 mins 384.0 MiB
Generated code and dependency injection configuration successfully.


  [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

I'm trying to test some functionality and made a quick command line to test it. My entire class is as follows:

<?php
namespace Falak\MyCommands\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CustomerCredit extends Command
{

    protected $transaction;
    protected $appState;

    public function __construct
    (
        \Magestore\Customercredit\Model\Transaction $transaction,
        \Magento\Framework\App\State $appState,
        array $data = []
    )
    {
        $this->appState = $appState;
        $this->transaction = $transaction;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('falak:credit')
            ->setDescription('Test Customer Credit');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $originalArea = $this->appState->getAreaCode();
        $this->appState->setAreaCode('frontend');
        $output->writeln("Hello World!");
        $this->appState->setAreaCode($originalArea);
    }


}

Notes:

  1. I have tried setting the area code in the constructor directly
  2. I have tried setting it in the configure
  3. I have tried to set it every where at the same time
  4. When I remove all reference to \Magestore\Customercredit\Model\Transaction it compiles without any problems. But I need this reference as I'm trying to call some of the functions in this class

UPDATE: Here is my di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="commandTest" xsi:type="object">Falak\MyCommands\Console\CommandTest</item>
                <item name="testCustomerCredit" xsi:type="object">Falak\MyCommands\Console\CustomerCredit</item>
            </argument>
        </arguments>
    </type>
</config>
  • Did you define di.xml file in etc folder? – akgola Sep 24 '18 at 7:26
0

Add in your construct like this :

public function __construct(
    \Magento\Framework\App\State $appState,
    $name=null
)
{
    $appState->setAreaCode('frontend');
    parent::__construct($name);
}

Remove var and generation folder.

  • Thanks for the answer. As I mentioned in the notes, I have already tried this with the same result – Hassan Al-Jeshi Sep 24 '18 at 7:37
0

Please define di.xml file in Module Falak\MyCommands\etc folder and put below code

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="falak:credit" xsi:type="object">Falak\MyCommands\Console\CustomerCredit</item>
            </argument>
        </arguments>
    </type>
</config>
</type>

Now clean all cache and run setup:di:compile and let me know if you still have issue.

You are also missing code to get area code in protected function execute():

try {
        $areaCode = $this->appState->getAreaCode();
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        // if area code is not set then magento generate exception "LocalizedException"
        $this->appState->setAreaCode('adminhtml');
    }
  • I have updated my question with the di.xml file I'm already using. I have also tried moving it completely and using your di.xml with the same result (without the last </type> as I think it's a typo) – Hassan Al-Jeshi Sep 24 '18 at 7:45
  • Now check my updated answer and let me know.If you are still facing issue just move di.xml file in etc/frontend folder and run command again. – akgola Sep 24 '18 at 7:52
  • Unfortunately, I have tried with the same result. Which puzzles me as I expect this to work since now I'm catching the LocalizedException. But it's still being thrown when I compile. – Hassan Al-Jeshi Sep 24 '18 at 8:06
  • 1
    Ok,I think it seems another issue.Please follow this extension as this have same functinality github.com/olegkoval/magento2-regenerate_url_rewrites – akgola Sep 24 '18 at 8:09
  • Hello @HassanAl-Jeshi ,is your issue resolved? – akgola Sep 24 '18 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.