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:

<?php
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
MageDispatch.com (ad)

Mage Dispatch is a newsletter for the community and by the community. Here you can share links that you think that the community should know about. We will include it in our next newsletter.

Want to respond?