How do I run custom script from magento2 root folder?
Question
Below url throwing Class customScript does not exist error. my test.php and customScript.php are in test folder. Once i run http://127.0.0.1/magento2/test.php, getting "Class customScript does not exist error" .
Here is my test.php code.
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
And in customScript.php
<?php
class customScript extends \Magento\Framework\App\Http implements
\Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 11588;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
How do I run php custom script on magento2
Please anyone able to achieve this?
Solution
Here is the code you need to use for test.php
<?php
require DIR . '../../app/bootstrap.php';
require dirname(__FILE__) . '/customScript.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
OTHER TIPS
Please try below code to run external script in magento2 :
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo "test custom script";
?>
Use below code to run a custom script from Magento 2 root folder
<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
require dirname(__FILE__) . '/customScript.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
Use the below code to run a custom script on Magento 2 root folder.
<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$securedArea = $objectManager->get('\Magento\Framework\Registry');
$securedArea->register('isSecureArea', true);
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('global');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory->create();
$productCollection->addAttributeToSelect('image');
$productCollection
->setPageSize(20) // only get 10 products
->setCurPage(1) // first page (means limit 0,10)
->load();
foreach($productCollection as $product){
echo $sku = $product->getSku();echo "<br>";
}
?>
If the pub folder is your Magento's root folder then use the following custom script and you need to create it inside pub diectory.
<?php
use Magento\Framework\Config\ConfigOptionsListConstants;
require __DIR__ . '/../app/bootstrap.php';
$objectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, []);
$objectManager = $objectManagerFactory->create([]);
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory->create();
$productCollection->addAttributeToSelect('image');
$productCollection
->setPageSize(20) // only get 10 products
->setCurPage(1) // first page (means limit 0,10)
->load();
foreach($productCollection as $product){
echo $sku = $product->getSku();echo "<br>";
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange