How to run Custom Script code outside of Magento 2


In this Magento 2 tutorial, we will learn how to run custom script code outside of the Magento 2. You can follow the steps in this tutorial to understand how to do it.

As you may know, in Magento 1, many developers are used to with run custom file outside Magento project or root of project directory for testing or deployed of code purpose.

It’s quite simple in Magento 1 with just include Mage.php file:

In Magento 2, Application initiates with entry point bootstrap.php.

Let’s begin with step by step and understand.

Step 1

Let’s create a new file in Root folder of Magento 2 projects. In my case it’s test.php.

Step 2

Create another file in the same location called DemoApplication.php. And add the following code into it:

Step 3

That’s it, now point your Magento project into the browser and run Test.php. if everything is fine their then you can see Product model name inside the browser.

Inside DemoApplication.php, CreateApplication method comes from a bootstrap class that’s most important part of Magento 2 initiate application. It creates an instance of an application class. createApplication expects an implementation of the \Magento\Framework\AppInterface.

Lunch()  method is called by \Magento\Framework\App\Bootstrap::run and this is main part that initiate Magento 2 environment. And finally called

For more information on how to Magento 2 application initialization. You can refer below link to find more information.

This tutorial was intended by Magento web development professionals to make you learn about how to run custom script outside Magento project. You can query them and clear your doubts about this tutorial via comments.

We hope you find this Magento explorer tutorial helpful. Thanks.

Rate this post


About Author

James Warner - Experienced and Sr Magento Certified Developer as well as offshore software developer at NexSoftSys. He is an expert in Magento web development and bright technology knowledge to develop IT business system which includes user friendly access and advanced features.


  1. Very interesting informative article. Running custom script code outside of Magento 2 seems to be easy in this tutorial.

  2. Vivek Shingala on


    Thanks for this tutorial.

    I am getting this error while running the script.

    ReflectionException: Class createCategoryTree does not exist in D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Reader\ClassReader.php:19 Stack trace: #0 D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Reader\ClassReader.php(19): ReflectionClass->__construct(‘createCategoryT…’) #1 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Definition\Runtime.php(44): Magento\Framework\Code\Reader\ClassReader->getConstructor(‘createCategoryT…’) #2 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(71): Magento\Framework\ObjectManager\Definition\Runtime->getParameters(‘createCategoryT…’) #3 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create(‘createCategoryT…’, Array) #4 D:\xampp\htdocs\magento2\vendor\magento\framework\App\Bootstrap.php(233): Magento\Framework\ObjectManager\ObjectManager->create(‘createCategoryT…’, Array) #5 D:\xampp\htdocs\magento2\customScript.php(5): Magento\Framework\App\Bootstrap->createApplication(‘createCategoryT…’) #6 {main}

    Here, createCategoryTree class is same as DemoApplication class.

  3. Hey,

    Thanks small adjustments:
    (‘ DemoApplication ‘);

    Make it: (‘DemoApplication’);

    echo get_class($this->_objectManager->create(‘\Magento\Catalog\Model\Product));

    You forgot a quote should be: echo get_class($this->_objectManager->create(‘\Magento\Catalog\Model\Product’));

Leave A Reply