magento2-regenurl icon indicating copy to clipboard operation
magento2-regenurl copied to clipboard

Area code is not set

Open kunzi opened this issue 8 years ago • 10 comments

I tried to run the regen, but it resulted the following error:

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

kunzi avatar Aug 22 '17 08:08 kunzi

Trying today on Mageno 2.1.7 | PHP 7 Same issue.

jmellon avatar Aug 25 '17 05:08 jmellon

Same error here magento 2.1.8

jordanvector avatar Aug 28 '17 21:08 jordanvector

Same error here magento 2.1.8

statuscue avatar Aug 30 '17 14:08 statuscue

yeah, 2.1.9, Same issue.

spyrule avatar Oct 17 '17 20:10 spyrule

Confirmed - issue is happening on 2.1.9 too.

dcwilkinson avatar Oct 18 '17 08:10 dcwilkinson

@spyrule @samuidavid See the PR: https://github.com/Iazel/magento2-regenurl/pull/21

I changed de code like this PR and now is working, I don't know why it has not been merged yet.

cagartner avatar Oct 18 '17 12:10 cagartner

I fix this issure: https://github.com/py4762013/magento2-regenurl

yekyo avatar Nov 02 '17 09:11 yekyo

This is fixed in my fork, amongst other issues https://github.com/peterjaap/magento2-regenurl

peterjaap avatar Feb 22 '18 16:02 peterjaap

@py4762013 & @peterjaap - Could you consider submitting a PR rather than forking an entirely new repo? It would help everyone contribute to a single source and keep things much easier to manage for the community :)

TL;DR; - There is a simple fix all the way at the bottom that will alleviate this error.

Tracking down the source of the issue - We are running into a problem on the getAreaCode() method inside execute().

% magento iazel:regenurl -vvv


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


Exception trace:
 () at /home/vagrant/public_html/vendor/magento/framework/App/State.php:152
 Magento\Framework\App\State->getAreaCode() at /home/vagrant/public_html/app/code/Iazel/RegenProductUrl/Console/Command/RegenerateProductUrlCommand.php:71

Magento will throw an exception if the area code isn't set: In \Magento\Framework\App\State.php:

public function getAreaCode()
{
    if (!isset($this->_areaCode)) {
        throw new \Magento\Framework\Exception\LocalizedException(
            new \Magento\Framework\Phrase('Area code is not set')
        );
    }
    return $this->_areaCode;
}

Which makes the if (! $this->state->getAreaCode()) { conditional worthless here.

The values for area codes are set in Magento\Framework\App\Area.

const AREA_GLOBAL = 'global';
const AREA_FRONTEND = 'frontend';
const AREA_ADMINHTML = 'adminhtml';
const AREA_DOC = 'doc';
const AREA_CRONTAB = 'crontab';
const AREA_WEBAPI_REST = 'webapi_rest';
const AREA_WEBAPI_SOAP = 'webapi_soap';

/**
 * @deprecated
 */
const AREA_ADMIN    = 'admin';

I personally think that the best solution would be to allow for a CLI option to set which area code you'd like to use. I would assume the best 3 would AREA_GLOBAL, AREA_FRONTEND & AREA_ADMINHTML.

TL;DR;

For a quick fix, I replaced the conditional check for getAreaCode() within the exceute() function in RegenerageProductUrlCommand.php. Replace:

 if (!$this->state->getAreaCode()) {
     $this->state->setAreaCode('admin');
 }

With:

 $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);

And the command proceeded to work without throwing an exception.

With all that being said, my URL's still did not regenerate correctly :)

dambrogia avatar Mar 29 '18 17:03 dambrogia

@dambrogia I would consider it but this repo seems abandoned (10 outstanding pull requests and no sign from the repo owner for months). So it seems like a waste of time. Use my fork instead, that one works.

peterjaap avatar Mar 30 '18 08:03 peterjaap