0

with the help of How can I bootstrap Magento 2 in a test.php script?

i created files its working fine, but when i try to access admin code it through error "Area code is not Set"

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class Getapp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $datas = $this->_objectManager->create('Magento\ImportExport\Model\ResourceModel\History\Collection');

        foreach($datas as $data){
            print_r($data->getData()); //.'<br>';
        }

    }
}

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


how to access admin level code in magento2

Pradeep Kumar
  • 8,731
  • 12
  • 60
  • 86

1 Answers1

2

I found the solution in abstract file, set the area code with:

$this->_state->setAreaCode('adminhtml');  //frontend 
$this->_areaList->getArea($this->_state->getAreaCode())->load();

abstract.php

<?php
use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;

use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;

abstract class AbstractApp implements AppInterface
{
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        State $state,
        Filesystem $filesystem,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_state->setAreaCode('adminhtml');  //frontend 
        $this->_filesystem = $filesystem;
        $this->registry = $registry;
    }

    public function launch()
    {
        $this->run();
        return $this->_response;
    }

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}
Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421
Pradeep Kumar
  • 8,731
  • 12
  • 60
  • 86