Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Area code is not set #20

Open
kunzi opened this issue Aug 22, 2017 · 10 comments
Open

Area code is not set #20

kunzi opened this issue Aug 22, 2017 · 10 comments

Comments

@kunzi
Copy link

kunzi commented Aug 22, 2017

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

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

@jmellon
Copy link

jmellon commented Aug 25, 2017

Trying today on Mageno 2.1.7 | PHP 7 Same issue.

@jordanvector
Copy link

Same error here magento 2.1.8

1 similar comment
@statuscue
Copy link

Same error here magento 2.1.8

ericrisler added a commit to myntpartners/magento2-regenurl that referenced this issue Aug 30, 2017
ericrisler added a commit to myntpartners/magento2-regenurl that referenced this issue Aug 30, 2017
@spyrule
Copy link

spyrule commented Oct 17, 2017

yeah, 2.1.9, Same issue.

@dcwilkinson
Copy link

dcwilkinson commented Oct 18, 2017

Confirmed - issue is happening on 2.1.9 too.

@cagartner
Copy link

@spyrule @samuidavid See the PR: #21

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

@yekyo
Copy link

yekyo commented Nov 2, 2017

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

@peterjaap
Copy link

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

@dambrogia
Copy link

@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 :)

@peterjaap
Copy link

peterjaap commented Mar 30, 2018

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants