Hello Everyone,
In this blog, we will learn about how to Create Root Script in Magento 2.
Magento 2 Root Script is a method to print something, or get data using Object Manager.
As a Magento 2 developer, you often need to run such a script to manipulate data into your Magento 2 database.
Root Script is a time saver for Developers.
Root Script is used only for debugging purposes.
STEPS FOR CREATE ROOT SCRIPT IN MAGENTO 2
Step 1: Create test.php file
You can give any name to the root script file here. We created a test.php file.
[magento root directory]/pub/test.php
<?php
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . ‘/../app/bootstrap.php’;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(‘Magento\Framework\App\State’);
$state->setAreaCode(‘frontend’);
echo ‘Test Script’;
// Do Your Logic here using Object Manager
Note: Make sure that __DIR__ . ‘/../app/bootstrap.php is the correct path to app/bootstrap.php file.
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Create Root Script in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.