create test.php file in magento2 root, i want require baseUrl also i try to

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

but it's not work


It's work

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$obj->get('\Magento\Framework\App\State')->setAreaCode('frontend'); // for remove Area code is not set error
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
echo $baseUrl=$storeManager->getStore()->getBaseUrl();
  • It's work but it will give error like Area code is not set in – Vaibhav Ahalpara Feb 14 '17 at 7:00
$objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager   = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$baseUrl        = $storeManager->getStore()->getBaseUrl();

OR To get base dir path use ::

$myModuleDir = BP . '/app/code/MyVendor/MyModule';

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