2

This question already has an answer here:

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

marked as duplicate by Fabian Schmengler, Rakesh Jesadiya, Raphael at Digital Pianism magento2 Feb 14 '17 at 8:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

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
1
$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.