Categories


Archives


Recent Posts


Categories


Creating Magento 2 Admin Routes with Pestle

astorm

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!

Creating an MCV/MVVM landing page for Magento 2’s frontend cart application is relatively simple. You create a routes.xml file so your module can “claim” a frontname, then then create a controller class that’s named correctly to catch a particular URL pattern. This is even easier with pestle, my PHP CLI module framework and Magento code generation tool.

$ pestle.phar generate_route Pulsestorm_HelloGenerate frontend pulsestorm_hellogenerate

Admin landing pages, however, are a bit trickier. That’s because, in addition to a standard routes.xml and controller file, you also need to contend with

  1. Every Admin URL needs a nonce/key to prevent XSS attacks, which means adding a formal “Admin Menu Item” to the system
  2. Every formal “Admin Menu Item” needs to be associated with a User Role/Access Control Rule
  3. Every Admin controller needs to be associated with a User Role/Access Control Rule (often the same as the menu)

The Formal Menu Item means a link that shows up in a left side navigation sub-menu.

Today I’ve pushed some changes to pestle that ensure you can use it to create an admin route. The following commands will create a module named Pulsestorm_HelloAdmin, and add an ACL protected link under the System -> Other Settings menu

$ pestle.phar generate_module Pulsestorm HelloAdmin2 0.0.1
$ pestle.phar generate_acl Pulsestorm_HelloAdmin2 Pulsestorm_HelloAdmin2::top,Pulsestorm_HelloAdmin2::config
$ pestle.phar generate_route Pulsestorm_HelloAdmin2 adminhtml pulsestorm_helloadmin2 
$ pestle.phar generate_menu Pulsestorm_HelloAdmin2 Magento_Backend::system_other_settings Pulsestorm_HelloAdmin2::pulsestorm_helloadmin2_index_index Pulsestorm_HelloAdmin2::pulsestorm_helloadmin2_index_index "Link to Hello Admin Landing Page" "pulsestorm_helloadmin2/index/index" 10 

I’m working on a tenth article for my Magento 2 for PHP Developers series that will cover what these commands do, and what Magento uses each created file is for. However, until that’s written, the above should be enough to get you going on your admin modules, and don’t forget that all arguments to a pestle command are interactive – running pestle.phar generate_menu without arguments should help you understand what each entered value is for.

Finally – there’s two things you’ll still need to do manually

  1. Enter titles for your new ACL rules in the generated acl.xml file file
  2. Enter the ACL rule ID in the generated _isAllowed controller method

As always, if you run into any trouble or see room for improvement, GitHub issues are welcomed.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 29th April 2016

email hidden; JavaScript is required