profile

M Bytes

🟧 M Bytes #4: Debug complex PHP with conditional breakpoints


Hey Reader,

I've been hard at work preparing for my Xdebug workshop at MMFL 2025, and what's great about this prep is that I'll also have a ton of lessons coming on the way for debugging PHP with Xdebug. The first is below 👇

These atomic lessons are just meant to provide you with a small taste of the kind of content that I create. If you're interested in diving way deeper into topics like this, then you'll definitely want to check out the University.

Keep coding,

Mark
Teacher, M.academy
Say hi 👋 on LinkedIn & YouTube

Create a custom route in Magento

Let’s add a custom route to handle blog URLs in Magento. It’s simpler than you might think.

First, create a routes.xml file in your module’s etc/frontend directory. This file tells Magento how to handle specific URL patterns.

The core of your route configuration lives in the “router” node. Set its ID to “standard” to use Magento’s default frontend router.

Your route definition needs two key pieces: a frontName that matches your URL path (like “blog”), and a unique ID to identify the route in the system, which is usually the same as the frontName. But while frontName's can use dashes for URLs, the route ID needs to stick to letters, numbers, and underscores.

Finally, tell Magento which module handles these requests by adding a module node with your module’s name. Now when someone hits /blog, Magento knows exactly where to send them.

Execute a command in a Docker container

Docker makes it easy to run commands inside your containers with the docker exec command.

First, you’ll need to know which container to target. Run “docker ps” or “docker container ls” to see what’s currently running.

The basic syntax is straightforward: “docker exec”, followed by your container name and the command you want to run.

Want to hop right into a container’s bash shell? Use “docker exec -it container-name bash”. The “-it” flags keep things interactive and give you a proper terminal experience.

Remember that containers need to be running before you can exec into them. If your command isn’t working, double-check that your container is actually up.

When you’re finished working in the container, just type “exit” to return to your host system.

Create conditional breakpoints in PhpStorm with Xdebug

Conditional breakpoints are a powerful debugging tool when you need to pause execution only in specific scenarios.

Setting one up in PhpStorm is straightforward. First, create a regular breakpoint by clicking in the left gutter.

Then right-click that breakpoint dot and you’ll see a condition field. This is where the magic happens!

You can enter any valid PHP expression here. For instance, “$total > 100” would only pause execution when processing orders over $100.

These conditions can be as simple or complex as you need.

Want to debug a specific order?

  • Use “$order[‘id’] === 2”.

Need to catch orders with many items?

  • Try “count($order[‘items’]) > 3”.

Just remember to keep your conditions simple and on a single line. Avoid variable assignments — they won’t work here.


Working on enterprise Magento projects?

University membership gives you access to advanced courses covering scalability, performance, and enterprise-grade architecture patterns.


M Bytes

Join 9,000+ developers and get three free video lessons every week.

Share this page