Misalnya, untuk mendapatkan nama produk dengan skrip khusus
Contoh 1:
Buat test.php
di root Magentovar/www/html/magento2/test.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$id = 1;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $product->getName();
Anda dapat menjalankan skrip test.php
dengan
http://127.0.0.1/magento2/test.php
Contoh 2:
langkah 1: buat index.php di root magento 2
var/www/htmlmagento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
langkah 2: buat customScript.php
/var/www/html/magento2/test/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 = 12;
$_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;
}
}
Sekarang Anda dapat menjalankan skrip khusus ini dengan
http://127.0.0.1/magento2/test/