Debugging is a critical part of the development process for any ecommerce store. Professional developers understand this and prepare themselves for the task from the very start.
Some developers log the whole process and then start debugging, while others prefer debugging during the runtime – both are a great way to clean your ecommerce store off errors.
As Magento 2 is a newer version of Magento, debugging it is a little different than Magento 1. There are multiple ways to debug Magento 2 code in a store, and I will discuss how you can do it with ease to achieve the desired results.
Let’s start with the essentials first.
Magento 2 Development Modes
In Magento 2, there are three types of development modes available. These are:
- Default Mode
- Developer Mode
- Production Mode
To debug Magento 2 store, developers use the Magento 2 Developer Mode. This mode displays errors directly on the screen. In fact, it is a convenient way to debug even failed modules that show a blank page or 500 error on the screen. To enable the developer mode, just make the SSH connection to your server and run the following Magento 2 CLI Command on the root directory of your Magento 2 store:
php bin/magento deploy:mode:set developer
Optimize Your Store Performance!
Identify and resolve issues faster with expert support and quality cloud hosting for your Magento 2 Store with Cloudways.
Enable PHP Display Errors to Debug Magento Store
Magento 2 has disabled the PHP display errors in the production mode for obvious security reasons. However, you can enable these display errors for debugging purpose.
To enable PHP display error reporting, open the $Magento2Root/app/bootstrap.php file and un-comment the ini_set(‘display_errors’, 1); line.
Here ‘1’ means the display_errors option is now set to ‘On.’ This will display PHP errors encountered while loading the Magento 2 store in the browser.
How to Enable Magento 2 Display Errors for Magento 2 Debug
When an error occurs, Magento 2 logs it in the var/reports file and does not display it on the screen. However, you can get it to post the error on your screen for debugging. To do so, simply enable it from $Magento2Root/pub/errors/ directory by renaming the local.xml.sample file to local.xml.
Here’s the code for local.xml.sample:
<?xml version="1.0"?> <!-- /**  * Copyright © Magento, Inc. All rights reserved.  * See COPYING.txt for license details.  */ --> <config>    <skin>default</skin>    <report>        <!--            "action" can be set to "print" to show exception on screen and "email"            to send exception on specified email        -->        <action>print</action>        <!--            in "subject" you can set subject of email        -->        <subject>Store Debug Information</subject>        <!--            "email_address" admin email address        -->        <email_address></email_address>        <!--            "trash" is handle about trace info            value "leave" is for store on disk            value "delete" is for cleaning        -->        <trash>leave</trash>    </report> </config>
Enable ‘Template Path Hints
The ‘Template path hints’ is a diagnostic tool that adds notation with the path on each template of Magento 2 platform. Users can enable it for the storefront and the Admin panel through the configuration menu.
To enable template path hints for debugging Magento 2 store, just login to your Magento 2 Admin panel, navigate to STORES > Configuration. On the left panel, under the ADVANCED section, click Developer. Extend Debug from the right menu and set Enabled Template Path Hints for Storefront, Enabled Template Path Hints for Admin, and Add Block Names to Hints to YES.
Save the configuration, and you will immediately notice changes on both the frontend and backend of the Magento 2 ecommerce store.
Xdebug and PhpStorm combination
The combination of Xdebug and PhpStorm is one of the best ways to debug Magento 2 store. The main advantage of this method is that it adds breakpoints to the installation process and can help the developer review and change variables at any given time.
To set up Magento 2 Xdebug, you need to configure a file in your Git repo or configure it in your environment. Next, configure PhpStorm as your IDE, and set up port forwarding.
This official Magento document provides more help on the topic of how to install and configure Xdebug with PhpStorm for debugging Magento 2 store.
Conclusion
The article has covered all the detials that are needed to debug Magento 2 store along with the combination of Xdebug and PhpStorm the developers life would be much easy. I believe this information is enough to get you started with debugging a Magento 2 store on your own. If you have any questions, leave them in the comments below, and I will respond as soon as I can.
Abdur Rahman
Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]