How To Create Sample File On Root And Call Magento 2 Functions?

  • author-img Nidhi Arora
  • 5 years

With Magento 1, it was easy for every Magento developer to attain the best possible results in minimum possible coding efforts. For example, it requires to add “dirty” code to instantiate the “Mage_Core_Mode1_App” class for test purposes, which further makes it very easy to call test.php.

//some settings
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
//instantiate the app model
//my toy code in here.

However, as you all know that Magento 2.0 comes with a new code base, so it is now quite different and not so easy to create a sample file on root and call Magento 2 functions. That’s why our Magento tech eCommerce specialists have come up with an easiest solution for you after digging deeper into the technical details. Read on to learn step by step in a very simple and easy way:

Step 1: Create a file named as “test.php” in the root of your Magento instance.

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

Step 2: Now, create another file termed “TestApp.php” in the same location with the following:

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;


Step 3: Now, you can easily call “test.php” in your browser to execute everything under “TestApp::launch()”

More Explanation:

“createApplication” mode under bootstrap class plays the most significant role. It helps in producing an instance of an application class and anticipates an implementation of \Magento\Framework\AppInterface which involves two methods. Thus, it is better to create your own class in “TestApp” which implements such interface.

However, if you don’t want that your app handles exceptions, create a method “catchException” and return “false”. If you get stuck in a wrong situation, make sure to print it on the screen.

Now, you need to implement the method “launch” which is called via \Magento\Framework\App\Bootstrap::run. There is nothing matter which application is passed as a parameter as “run” mode does the same job. The only thing that matters is – ““$response = $application->launch();”

It is clear that when you call “\Magento\Framework\App\Bootstrap::run” it will initialize the Magento env and call the “launch” mode under the application. This is the reason why you should keep your all dirty code inside that mode. At that time, the “\Magento\Framework\App\Bootstrap::run” calls to “$response->sendResponse();” where “$response” is returned by the “launch” mode.

On the other hand, if you are creating your class extend nothing, you should copy your constructor from the “\Magento\Framework\App\Http” class. There may be a need to add more parameters in your constructor as per your own requirements.

And that’s it. Hope it will help you to create sample file on root and call Magento2 functions. However, if you still face any difficulty, you can contact us, our certified Magento developers will serve you with best possible Magento 2 solutions


Download Blog


Ready to Get started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    1250 Pittsford Victor Road Ste 310
    Pittsford, NY 14534

    Development Center

    Plot no. 10, Rajiv Gandhi
    Chandigarh Technology Park