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:
1 | require_once ‘app/Mage.php’; |
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.
1 2 3 4 5 6 | <?php require __DIR__ . '/app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ $app = $bootstrap->createApplication('DemoApplication'); $bootstrap->run($app); |
Step 2
Create another file in the same location called DemoApplication.php. And add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php class DemoApplication extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface { public function launch() { //here I called object name of product for print on browser //you can place your logic here echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Product)); //below method use to return response return $this->_response; } public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception) { return false; } } |
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
1 | $response = $application->launch(); |
For more information on how to Magento 2 application initialization. You can refer below link to find more information.
http://devdocs.magento.com/guides/v2.0/config-guide/bootstrap/magento-bootstrap.html
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.
5 Comments
Very interesting informative article. Running custom script code outside of Magento 2 seems to be easy in this tutorial.
Hi
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.
Hi.. Did you able to solve this exception? I am also facing the same issue after running the script.
Hey,
Thanks small adjustments:
(‘ DemoApplication ‘);
Make it: (‘DemoApplication’);
And
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’));
Thank you, I have edited the error.