1

I need to get admin values to a PHP file located in Magento 2 root folder.

In Magento 1 I could do it by:

require ('./../../app/Mage.php');

How can I do this in Magento 2?

4
  • 1
    What do you mean by admin values? You want to access magento code in custom script ?
    – Keyur Shah
    Commented Jan 15, 2018 at 18:27
  • Sorry, I meant admin config values. Or calling a helper for that matter. But yes I want to access magento code in custom script Commented Jan 15, 2018 at 18:28
  • You mean store config values ?
    – Keyur Shah
    Commented Jan 15, 2018 at 18:29
  • I know how to get the values I need, the problem is that the magento code is not recognized in the root folder Commented Jan 15, 2018 at 18:31

1 Answer 1

2
<?php

ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$config = $obj->create('Magento\Framework\App\Config\ScopeConfigInterface');
echo $config->getValue('path/to/config', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
exit;

You just need to replace path/to/config with your config path

5
  • $review should be $config ;) Commented Jan 15, 2018 at 18:53
  • but it works great, thanks :) Commented Jan 15, 2018 at 18:53
  • Yes, it is typo updated the answer :) @JurģisTomsLiepiņš
    – Keyur Shah
    Commented Jan 15, 2018 at 18:53
  • Sorry, do you know how to get the value from a certain storeview with this method? Commented Jan 15, 2018 at 19:07
  • 1
    I added the answer into your question please check magento.stackexchange.com/a/209552/2589 @JurģisTomsLiepiņš
    – Keyur Shah
    Commented Jan 15, 2018 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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