Categories


Archives


Recent Posts


Categories


Magento 2: Controller Class Validation

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!

If you’ve spent anytime doing Magento 1 module development, you’re probably familiar with the _validateControllerClassName method. This method is where Magento 1 generates and validates paths to possible controller class matches during routing.

This method is gone in Magento 2 – partially due to the PSR-0 autoloader being the standard, even for controller classes, and partially due to refactoring.

Here’s a few similar spots in Magento 2 that can help you track down a non-matching route configuration – you may want to read through my article on Magento 2’s routing and M-V-VM system if you’re a little unclear on what we’re talking about.

First, there’s Magento/Framework/App/Router/Base, where Magento searches for a module match. If your module doesn’t show up in $modules, something’s amiss.

#File: lib/internal/Magento/Framework/App/Router/Base.php
protected function matchAction(MagentoFrameworkAppRequestInterface $request, array $params)
{
    //
    $modules = $this->_routeConfig->getModulesByFrontName($moduleFrontName);
    //
}    

Then there’s MagentoFrameworkAppRouterActionList

#File: lib/internal/Magento/Framework/App/Router/ActionList.php
public function get($module, $area, $namespace, $action)
{
    //...
    $fullPath = str_replace(
        '_',
        '\',
        strtolower(
            $module . '\controller' . $area . '\' . $namespace . '\' . $action
        )
    );

    if (isset($this->actions[$fullPath])) {
        return is_subclass_of($this->actions[$fullPath], $this->actionInterface) ? $this->actions[$fullPath] : null;
    }    
    //...        
}

Checking the value of $this->actions and $fullPath can help you find common typo mistakes.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 26th September 2015

email hidden; JavaScript is required