How can you bootstrap Magento 2 in the test.php script

Magento 2 is the advanced featured version of Magento 1. If we can compare Magento 2 with Magento 1 then, Magento 2 is faster, user-friendly, and SEO-friendly than Magento 1. Moreover, there are some tasks in Magento 1 and 2 that are done with the same steps. But some can be done with different methods. So today we will learn how can you bootstrap Magento 2 in the test.php script.

Further, in Magento 1, you can create a file where you need to instantiate the Mage_Core_Model_App class. After that, you can add any of your code for test purposes. But in Magento 2 it is different. Let's see.

Steps to Bootstrap Magento 2 in the test.php Script

Firstly, create a test.php file in the Magento 2 root directory.

<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

After that, create a Testapp.php file at the same place and paste the following code there.

<?php
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\Category'));
        //the method must end with this line
        return $this->_response;
    }

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

}

Then, call the test.php file in the browser. And that will execute TestApp::launch().

How this will work

The "createApplication" method is an important method in the bootstrap class because it helps to create an instance of an application class. This method awaits implementation of the \Magento\Framework\AppInterface that contains 2 methods.

Therefore, we have created our class in TestApp that implements the interface. To avoid exceptions we have created "catchException" that returns false always. Whenever something went wrong, print this.

After that, we have implemented a launch function called by  \Magento\Framework\App\Bootstrap::run. No matter what application passes as a parameter, the run method will handle it. The only line that depends on the application is

$response = $application->launch();

So, calling \Magento\Framework\App\Bootstrap::run will init the Magento env and call the launch method from the application. This is the reason why you should add your code to that method.

After that, the \Magento\Framework\App\Bootstrap::run calls $response->sendResponse(); where $response is what the launch method returns. So it is important to return $this->_response;.

Moreover, you can make your application class extend the \Magento\Framework\App\Http so that request and response parameters are easily available.

However, you can also make your class extend nothing. To do that, you have to copy the constructor class from the \Magento\Framework\App\Http class. Additionally, you can add more parameters in the constructor class if necessary.

Conclusion

So, this is the most verified and almost working solution for how to bootstrap Magento 2 in the test.php script. Further, if this will not work either you can contact us or try the other solution. Therefore, I wish this article will helpful for you.

Additionally, here are some most useful articles for you.

How to get a free demo in Magento 2 extension?

Top 5 Magento 2 personalized product Extensions