Using a debug.php with Magento

Magento 2, Magento 2 testing, Magento 2 local environment

Sometimes I come into a situation where I need to run some specific code in the context of Magento to see what is happening. Now you can always create a Controller and place your code in there. Or you can execute the code using n98-magerun2 dev:console. Both are fine options but not always sufficient enough.

In those cases, I resort to a good old debug.php. But what has one todo to run Magento code from a single file? Let me show you:

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

// Your code here.

Now you dump the code in here and just run:

php debug.php

Want to respond?