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.

Its quite simple in magento 1 with just include Mage.php file:

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

Lets begin with step by step and understand.

Step 1

Lets create new file in Root folder of magento 2 project. In my case its test.php.

Step 2

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

Step 3

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

Inside DemoApplication.php, CreateApplication method comes from 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.

Hope you enjoy tutorial. 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 Comment

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

Leave A Reply