0

I am trying to write some business logic root folder of index.php file, but it's throwing below error.

Uncaught RuntimeException: ObjectManager isn't initialized in vendor\magento\framework\App\ObjectManager.php on line 31

I have a multi store based on Country I am redirecting store to appropriate. For these, I have done below code.

<?php
/**
 * Application entry point
 *
 * Example - run a particular store or website:
 * --------------------------------------------
 * require __DIR__ . '/app/bootstrap.php';
 * $params = $_SERVER;
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
 * $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
 * \/** @var \Magento\Framework\App\Http $app *\/
 * $app = $bootstrap->createApplication('Magento\Framework\App\Http');
 * $bootstrap->run($app);
 * --------------------------------------------
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

//$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$params = $_SERVER;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$getHelper = $objectManager->get('Learning\Location\Helper\Data');
$getStore = $getHelper->getCountry();

if($getStore == "india"){
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'default';
}else if($getStore == "united states"){
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'us';
}else{
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'us';
}
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store'; // store or website
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

How can I write my logic inside index.php? Please suggest us

1 Answer 1

1

Finally, worked for me using the below code.

<?php
/**
 * Application entry point
 *
 * Example - run a particular store or website:
 * --------------------------------------------
 * require __DIR__ . '/app/bootstrap.php';
 * $params = $_SERVER;
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'website2';
 * $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website';
 * $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
 * \/** @var \Magento\Framework\App\Http $app *\/
 * $app = $bootstrap->createApplication('Magento\Framework\App\Http');
 * $bootstrap->run($app);
 * --------------------------------------------
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo <<<HTML
<div style="font:12px/1.35em arial, helvetica, sans-serif;">
    <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
        <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
        Autoload error</h3>
    </div>
    <p>{$e->getMessage()}</p>
</div>
HTML;
    exit(1);
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$params = $_SERVER;

$objectManager = $bootstrap->getObjectManager();
$getHelper = $objectManager->get('Learning\Location\Helper\Data');
$getStore = $getHelper->getCountry();

if($getStore == "india"){
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'default';
}else if($getStore == "united states"){
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'us';
}else{
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'us';
}
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store'; // store or website
$bootstrap1 = \Magento\Framework\App\Bootstrap::create(BP, $params);



/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap1->createApplication('Magento\Framework\App\Http');
$bootstrap1->run($app);

Your Answer

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

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