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.

2 Answers 2

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');
6
  • Getting 404 not found error. screencast.com/t/fU8IM0X4 Commented Apr 18, 2019 at 6:11
  • @MuhammadAnas problem in file permission above code is working code Commented Apr 18, 2019 at 6:12
  • getting this error PHP Warning: include(app/bootstrap.php): failed to open stream: No such file or directory Commented Apr 18, 2019 at 6:16
  • still getting error PHP Warning: include(app/bootstrap.php): failed to open stream: No such file or directory Commented Apr 18, 2019 at 6:19
  • I don't know who gave negative on your answer. Is your answer is correct? Commented Apr 18, 2019 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******************
1
  • Rakesh Donga's answer is working fine. +1 for your effort though. Commented Apr 18, 2019 at 6:28

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.