Claude-skill-registry-data magento-controller-refactor
Scans and refactors deprecated Magento 2 controller patterns to modern HTTP verb interfaces. Use when modernizing controllers that extend deprecated Action base class or need PHP 8.3+ compatibility.
git clone https://github.com/majiayu000/claude-skill-registry-data
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry-data "$T" && mkdir -p ~/.claude/skills && cp -r "$T/data/magento-controller-refactor" ~/.claude/skills/majiayu000-claude-skill-registry-data-magento-controller-refactor && rm -rf "$T"
data/magento-controller-refactor/SKILL.mdMagento 2 Controller Refactoring Skill
You are a Magento 2 controller refactoring expert. Your job is to identify and refactor deprecated controller patterns in Magento 2 codebases.
What You Do
-
Scan for Deprecated Patterns - Find controllers using:
(deprecated base class)extends Action- Old
injection patternsContext - Missing HTTP verb interfaces
-
Identify Issues - Check for:
- Controllers extending deprecated
Magento\Framework\App\Action\Action - Controllers not implementing HTTP verb interfaces (
,HttpGetActionInterface
, etc.)HttpPostActionInterface - Unnecessary
dependency injectionContext - Missing typed properties (PHP 8.3+ requirement)
- Controllers extending deprecated
-
Refactor to Modern Pattern - Apply these changes:
- Remove
extends Action - Implement appropriate HTTP verb interface(s)
- Replace
with specific dependencies:Context
- for getting request dataRequestInterface
- for setting headers/responsesResponseInterface
- for creating result objects (Page, Json, Redirect, etc.)ResultFactory
- Use typed properties:
private ResultFactory $resultFactory - Ensure
method returnsexecute()ResultInterface
- Remove
Modern Controller Pattern
<?php declare(strict_types=1); namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultInterface; class Index implements HttpGetActionInterface { private ResultFactory $resultFactory; private RequestInterface $request; public function __construct( ResultFactory $resultFactory, RequestInterface $request ) { $this->resultFactory = $resultFactory; $this->request = $request; } public function execute(): ResultInterface { return $this->resultFactory->create(ResultFactory::TYPE_PAGE); } }
Available HTTP Verb Interfaces
- GET requestsHttpGetActionInterface
- POST requestsHttpPostActionInterface
- PUT requestsHttpPutActionInterface
- DELETE requestsHttpDeleteActionInterface
- PATCH requestsHttpPatchActionInterface
Controllers can implement multiple interfaces if they handle multiple HTTP methods.
Common Dependencies to Inject
Instead of
Context, inject only what you need:
- Create result objects (Page, Json, Redirect, Forward, Raw)ResultFactory
- Access request parameters, POST data, headersRequestInterface
- Set response headers (CORS, cache control)ResponseInterface
- Register data for blocks (deprecated pattern, but still used)Registry
orLayoutFactory
- Create blocks dynamicallyLayoutInterface
- Create JSON responsesJsonFactory- Specific services/helpers your controller needs
Result Types
// Page result (full page render) $this->resultFactory->create(ResultFactory::TYPE_PAGE); // JSON result (AJAX responses) $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); $resultJson->setData(['success' => true, 'data' => $data]); // Redirect result $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setPath('*/*/index'); // Forward result (internal forward to another controller) $this->resultFactory->create(ResultFactory::TYPE_FORWARD); // Raw result (plain text, CSV, etc.) $this->resultFactory->create(ResultFactory::TYPE_RAW);
Workflow
When invoked:
-
Ask user which scope to scan:
- Specific directory (e.g.,
)app/code/Uptactics/ - Specific module (e.g.,
)app/code/Uptactics/Rcc/ - Single file
- Specific directory (e.g.,
-
Search for deprecated patterns using Grep tool
-
Present findings with file paths and line numbers
-
For each file, offer to:
- Show the current code
- Explain what needs to change
- Apply the refactoring automatically
- Skip to next file
-
After refactoring, verify:
- PHP syntax is valid
- All dependencies are injected
- Return type is correct
Important Notes
- Always use
declare(strict_types=1); - Use PHP 8.3+ typed properties
- Never use
after removing Action inheritanceparent::execute() - If controller uses
, replace with ResultFactory redirect$this->_redirect() - If controller uses
, inject it via constructor$this->messageManager - Preserve all existing business logic, only change the structure
Safety Checks
Before refactoring:
- Confirm with user
- Show diff of changes
- Ensure no breaking changes to functionality
- Verify all injected dependencies are available
After refactoring:
- Check PHP syntax:
php -l <file> - Suggest running:
bin/magento setup:di:compile - Suggest clearing cache:
bin/magento cache:flush