I need to load product or order collection outside the magento, i need to play around the data so i need to call collection
in magento1.x we use

require_once '../app/Mage.php';
$products = Mage::getModel('catalog/product')->getCollection();

how to do it on Magneto2?


thank you all i got it

i followed as per the post How can I bootstrap Magento 2 in a test.php script?

in root folder i created script/abstract.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->_filesystem = $filesystem;
        $this->registry = $registry;

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

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
        return false;

and then script/index.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()
            ->register('isSecureArea', true);

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

       // $products = $this->_objectManager->create('\Sugarcode\Test\Model\Test'); // custom Module 
       //$products = $this->_objectManager->create('\Magento\Catalog\Model\Product');
     //   $products = $products->getCollection();
        foreach($datas as $data){
            echo $data->getIncrementId().'<br>';


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

so in index.php write your logic and url/script/index.php

it working fine

| improve this answer | |
  • Really like your DI aproach. – jrosell Sep 9 '16 at 11:12
  • then click on up arrow :-) – Pradeep Kumar Sep 28 '16 at 13:19
  • Already voted up ;) – jrosell Sep 29 '16 at 19:00
  • Is "script" your root folder, or do you edit the magento2s index.php? – Max Oct 9 '16 at 18:56
  • Instead of echoing how would you return $datas to $app from the run method? – paj Jul 3 '17 at 18:20

You can create you own file that instantiates the application, like explained in here:

And in that file you can try this:

$collection = $this->_objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');
| improve this answer | |
  • if i want use admin code then how to do it , it shows error "area code is not set", how to set area code and how for admin access – Pradeep Kumar Dec 3 '15 at 12:44

This is an unsupported scenario.
I recommend to use the official APIs for Magento 2: http://devdocs.magento.com/guides/v2.0/get-started/bk-get-started-api.html

| improve this answer | |
  • 4
    Please stop posting links to your website. If you have a solution, please post it here fully. – Marius Nov 23 '15 at 8:20
  • 99% of my dev work was playing in the 'test app' learning how magento works. – CarComp Mar 30 '16 at 19:23

In magento,you can do dirty ways:

So, check this question How can I bootstrap Magento 2 in a test.php script?

In magento product collection is generate from Magento\Catalog\Model\Resource\Product\CollectionFactory

For your requirement,you need to change at TestApp class

class TestApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {

    public function launch()
        //dirty code goes here. 
        //the example below just prints a class name
        echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Resource\Product\CollectionFactory'));
    // addtional:
            ->addUrlRewrite()   ;   
        //the method must end with this line
        return $this->_response;

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
        return false;

| improve this answer | |
  • where does this file go? can I put it under /app/pub? It doesn't seem to work for me when I put it there. – ahnbizcad Sep 5 '16 at 20:52

Magento Setup is an example of completely separate (Zend MVC) application in Magento core, which has access to Magento object manager. Good starting point to look at is <magento_project_root>/setup/index.php (the most important part here is to use Magento bootstrap).

| improve this answer | |
  • :- not use i saw that file , if i follow that it through error if init model ex;- $prdouct=new /Magento\Product\Model\Product(), it through error argument 1 is missing, so we need to follow as i posted below answer, because we need to include few need class with out that not possible – Pradeep Kumar Nov 23 '15 at 9:48

What worked for me was an answer from here, answering a question of how to get cart items. In the process, one must load magento, so two birds, one stone.



use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

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

$obj = $bootstrap->getObjectManager();

// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');

// Get whatever object
// example
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
| improve this answer | |

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.