15

I am receiving this error:

Uncaught exception 'InvalidArgumentException' with message 'Constant name is expected.

While running php bin/magento setup:upgrade in command line.

i got this http://awesomescreenshot.com/0535zi4648 error.

0

6 Answers 6

19

I think some problem in your etc folder. check all xml files. Constant name should be in CAPITAL letters. any of your constant remain in small letters. check it and correct it. Hope this will solve your Problem.

27

In order to help you debug what's going on, I would suggest to temporary edit the file

vendor/magento/framework/Data/Argument/Interpreter/Constant.php

From this...

public function evaluate(array $data) {
    if(!isset($data['value']) || !defined($data['value'])) {
        throw new \InvalidArgumentException('Constant name is expected.');
    }
    return constant($data['value']);
}

To this...

public function evaluate(array $data) {
    if(!isset($data['value']) || !defined($data['value'])) {
        print_r($data);exit;                                                    // <<<<<<<<<<<<
        throw new \InvalidArgumentException('Constant name is expected.');
    }
    return constant($data['value']);
}

Thereby you will be notified of which class / constant is causing you the error :

enter image description here

Hope it could help someone else ;-) See ya

2
  • Hello! I just got Array ( [name] => dateFormat [value] => \IntlDateFormatter::SHORT ), what can I do next?
    – Paul Fan
    Oct 26, 2020 at 21:31
  • I think your case may be particular because \IntlDateFormatter isn't part of Magento but more of a PHP extension. Without context I would say that "intl" is not installed but required by Magento : devdocs.magento.com/guides/v2.4/install-gde/prereq/….
    – rou6e
    Oct 27, 2020 at 1:09
17

Edit, extra information:

As explained below, please investigate folder names/case for your files, also make sure the class defined matches your file path. Incorrect folder case/names means the CONSTANT cannot be resolved based on the class where it is defined, in which case the error message might not initially be useful. See the other answers on which core file to override to get more information on the file causing the issue.

Original:

In the case when your extension is in app/code folder, you will get this error if your folder name is different than module name after Magento_ in module.xml

For example: If your module name is Magento_MyGreatModule your folder name must be MyGreatModule.

6
  • thank you ! another great example of misleading error msg ...
    – medmek
    Jun 23, 2018 at 15:46
  • This answer worked for me as well.
    – Arfeen
    Jul 26, 2018 at 10:03
  • Thank you! Bumped into this one after I renamed my module. Nov 27, 2019 at 10:57
  • 1
    @medmek, I don't think the error is misleading, sure when the 3rd party module doesn't have the correct folder structure as defined in their classes, this is troublesome to find and fix. Overriding the vendor/magento/framework/Data/Argument/Interpreter/Constant.php file and outputting the $data, and then using this answer in conjunction with the result it is clear why this error message is useless (but not misleading). They should have given more output for the error by default though. Oct 6, 2021 at 9:58
  • 1
    @CvRChameleon Indeed, thank you !
    – medmek
    Oct 6, 2021 at 10:02
4

Like already stated in the previous answers, this is most likely because some error in one of your configuration files. For instance, this error is triggered when you do the following without having a class that declares the constant (notice the ::CODE declaration):

<!-- etc/di.xml -->
<virtualType name="ExamplePaymentGatewayConfig" type="Magento\Payment\Gateway\Config\Config">
  <arguments>
    <argument name="methodCode" xsi:type="const">\Company\Example\Model\Ui\ConfigProvider::CODE</argument>
  </arguments>
</virtualType> 
0
1

Changing module's namespace without changing the actual directory name caused the issue on my end.

1
  • +1 Look for "CamelCase" in vendor name or module name. It should match what is specified in registration.php
    – Akif
    Jul 11 at 10:35
0

In my case the const CODE has wrong name and temporary modify the file

vendor/magento/framework/Data/Argument/Interpreter/Constant.php

Is helped me to find the name of the actual module.

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.