There may be occasions where you need to modify core Magento functionality using an external script. You might do this to import and export data from Magento or to add, modify or remove data quickly. To bootstrap and use Magento 2 in an external file requires a few lines of code to be added at the top of the file.
If you’ve bootstrapped Magento 1 using an external file in the past, you’ll know that you needed to include the Mage.php
using PHP’s include
or require
keywords. Then a Mage::app()
line was added below to initialise the Magento 1 application.
Should the external file exist in the Magento root directly, the contents of the file might have looked like the below.
<?php require_once __DIR__ . '/app/Mage.php'; Mage::app(); // Loading a product below will now work $product = Mage::getModel('catalog/product')->load(1); print_r($product->getData());
In Magento 2, instead of Mage.php
, include the app/bootstrap.php
file.
<?php require_once __DIR__ . '/app/bootstrap.php';
Now here’s where things get more different. Next, define a $bootstrap
variable that will store an instance of Magento’s bootstrap class, Magento\Framework\App\Bootstrap
.
You can do this by using the Bootstrap
class’ create()
static method.
<?php require_once __DIR__ . '/app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
You can then use the createApplication()
method of the Bootstrap
class to create an application to use.
This is then followed by the Bootstrap
class’ run()
method.
<?php require_once __DIR__ . '/app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ $app = $bootstrap->createApplication('TestApplication'); $bootstrap->run($app);
Note the TestApplication
string passed into createApplication()
. Magento will look for a TestApplication
class.
Therefore you need to add a class which will contain your custom code. You can define this class within the same file.
Ensure that the class is defined above the $app = $bootstrap->createApplication('TestApplication');
so that the class can be found by Magento.
The class should extend \Magento\Framework\App\Http
and implement \Magento\Framework\AppInterface
and contain a launch()
method that should return $this->_response
.
You code might look like the below example.
<?php require_once __DIR__ . '/app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); class TestApplication extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface { public function launch() { return $this->_response; } public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception) { return false; } } /** @var \Magento\Framework\App\Http $app */ $app = $bootstrap->createApplication('TestApplication'); $bootstrap->run($app);
Within the launch()
method, you can add in your code.
public function launch() { $logger = $this->_objectManager->get('Psr\Log\LoggerInterface'); $logger->critical('Hey! This works.'); return $this->_response; }
The example above will successfully log Hey! This works.
to Magento’s system.log
file.
If you need to modify data such as product data, ensure you set an area code to adminhtml
when bootstrapping the Magento 2 application.
To do this, use the Magento\Framework\App\State
class and the setAreaCode()
method.
$state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml');
The code below shows an example of loading a products whose ID is 1
and setting and saving the product’s name to Some Product Name
.
public function launch() { $state = $this->_objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); $productRepository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository'); $product = $productRepository->getById(1); $product->setName('Some Product Name'); $productRepository->save($product); print_r($product->getName()); // Some Product Name return $this->_response; }
When you bootstrap and use Magento 2 in an external file, don’t forget to remove the file from the root directory if you only need to run the file once. This ensures you don’t accidentally execute the code within it at a later date and cause unexpected updates to your database.
Note: This article is based on Magento Open Source version 2.2.