2

In Magento 1.9 I simply use below code and Magento's Mage model could be run on the script then

include_once 'App.php';
include_once __DIR__.'/../app/Mage.php';

How can I do it in Magento 2? So that I would be able to create custom script on root directory.

3

you can try this way..

<?php  
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
include(__DIR__.'/../app/bootstrap.php');

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

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
| improve this answer | |
  • Getting 404 not found error. screencast.com/t/fU8IM0X4 – Muhammad Anas Apr 18 '19 at 6:11
  • @MuhammadAnas problem in file permission above code is working code – Rakesh Donga Apr 18 '19 at 6:12
  • getting this error PHP Warning: include(app/bootstrap.php): failed to open stream: No such file or directory – Muhammad Anas Apr 18 '19 at 6:16
  • still getting error PHP Warning: include(app/bootstrap.php): failed to open stream: No such file or directory – Muhammad Anas Apr 18 '19 at 6:19
  • I don't know who gave negative on your answer. Is your answer is correct? – Muhammad Anas Apr 18 '19 at 6:21
1
<?php

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');


**********add your code******************
| improve this answer | |
  • Rakesh Donga's answer is working fine. +1 for your effort though. – Muhammad Anas Apr 18 '19 at 6:28

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.