Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento
front end book you'll ever need. Get your copy
today!

We interrupt your regularly scheduled Magento tutorial for a quick announcement about a new (Magento related project) I’ve been working on.

Pestle is a new PHP (5.6+) framework for creating and organizing PHP programs into python like libraries of functions. It’s a little outside the mainstream direction PHP’s heading these days, but if python like library of functions whispers to a secret part of your programming heart, checkout the wiki and your feedback is more than welcome.

If python like library of functions does not whisper to a secret part of your programming heart, the other half of pestle is a growing list of Magento 2 code generation functions. I was always a little disappointed that Magento’s community driven code generation tools have been focused around a kitchen sink approach, and pestle’s generate_ commands are a response to that.

Right now we have commands for generating a new module, a new route, a new view/block/layout for that route, a new observer, a new Magento CLI command (recursive!), a skeleton theme, a plugin’s configuration, and a command to automatically add the property, parameter, and assignment that injects a dependency into a class. We’re also keeping a running list of command suggestions, so if you have an idea or would like to work on an extension please head on over to GitHub.

If you’re not familiar with what code generation can do for you, the rest of this article explores the pestle command for creating a new module.

Creating the Module

If you’ve worked through the Introduction to Magento 2 — No More MVC, you know creating a basic Magento module is straight forward, but involved many different files. Pestle can handle the grunt work of creating those files for you.

For example — all you need to do to generate your module.xml boiler plate is run the following.

Adding a View

Now that we have a module and controller endpoint configured, we need to add Magento’s default view files. Once again, pestle can save us the hassle of needing to manually create our layout handle XML file, phtml template, and default block/view class.

After running the above, clear your cache and reload the page. You should see a fully laid out Magento page, with your view’s content right in the middle.

Powerful Scripting

In addition to helping developers automate the grunt work of a Magento module, it’s possible to specify any generate input as a command argument. This means you could recreate the above with a shell script that looks like this