Hey, need help with your Magento, WooCommerce or Laravel project? I have some availalility. Contact me.

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)

Hey, I'm running a developer focussed newsletter called MageDispatch.com, which is for the community and by the community. Here you can share links that you think that the community should know about. If you like this kind of technical content you will like this newsletter too.

Want to respond?