Create Magento 2 Module with Pestle

Pestle is a new PHP framework for creating and organizing PHP programs into Python like libraries of functions.

Posted on August 24, 2016 in Magento2

With this Framework, it’s super easy to create a new module in Magento 2 system by typing some commands. You can get the Pestle at the Pestle at Github.

Creating the Module

All you need to do generate your module.xml boiler plate is run the following commands.


$ pestle.phar generate_module
Vendor Namesapce? Jeff
Module Name? Helloworld
Version? 0.0.1

Created: /path/to/magento2/app/code/Jeff/Helloworld/etc/module.xml
Created: /path/to/magento2/app/code/Jeff/Helloworld/registration.php

Then enable it with Magento’s CLI Tool


$php bin/magento module:enable Jeff_Helloworld

$php bin/magento setup:upgrade

Adding the Route/URL Endpoint

We can add a URL controller endpoint by typing following commands.


pestle.phar generate_route
Which module? Jeff_Helloworld
Which area? [frontend, adminhtml] frontend
Frontname/Route ID? helloworld

/path/to/magento2/app/code/Jeff/Helloworld/etc/frontend/routes.xml
/path/to/magento2/app/code/jeff/Helloworld/Controller/Index/Index.php

Open the generated controller and add the following debudding code to the execute method to check router


#File: app/code/Jeff/Helloworld/Controller/Index/Index.php
public function execute() {
    var_dump(__METHOD__);
    return $this->resultPageFactory->create();
}

Then clear your magento cache and load the URL in your browser


http://yourdomain.com/helloworld

You should see an HTML page returned with HTTP Status: 200 and following content.


string 'Jeff\Helloworld\Controller\Index\Index::execute' 

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.


$ pestle.phar generate_view Jeff_Helloworld frontend helloworld_index_index Main content.phtml
$ pestle.phar generate_view
Which Module? Jeff_Helloworld
Which Area? frontend
Wich Handle? helloworld_index_index
Block Name? Main
Template File? content.phtml

Creating /path/to/magento2/app/code/Jeff/Helloworld/view/frontend/template/content.phtml
Creating Jeff\Helloworld\Block\Main
Creating /path/to/magento2/app/code/Jeff/Helloworld/view/frontend/layout/helloworld_index_index.xml

After running the above, clear your cache and reload the page. You should see a fully laid out Magento page.

You can get a full list of commands by running


$pestle.phar list


comments powered by Disqus