Magento 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:
extends Action (deprecated base class)
- Old
Context injection patterns
- Missing HTTP verb interfaces
Identify Issues - Check for:
- Controllers extending deprecated
Magento\Framework\App\Action\Action
- Controllers not implementing HTTP verb interfaces (
HttpGetActionInterface, HttpPostActionInterface, etc.)
- Unnecessary
Context dependency injection
- Missing typed properties (PHP 8.3+ requirement)
Refactor to Modern Pattern - Apply these changes:
- Remove
extends Action
- Implement appropriate HTTP verb interface(s)
- Replace
Context with specific dependencies:
RequestInterface - for getting request data
ResponseInterface - for setting headers/responses
ResultFactory - for creating result objects (Page, Json, Redirect, etc.)
- Use typed properties:
private ResultFactory $resultFactory
- Ensure
execute() method returns ResultInterface
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
HttpGetActionInterface - GET requests
HttpPostActionInterface - POST requests
HttpPutActionInterface - PUT requests
HttpDeleteActionInterface - DELETE requests
HttpPatchActionInterface - PATCH requests
Controllers can implement multiple interfaces if they handle multiple HTTP methods.
Common Dependencies to Inject
Instead of Context, inject only what you need:
ResultFactory - Create result objects (Page, Json, Redirect, Forward, Raw)
RequestInterface - Access request parameters, POST data, headers
ResponseInterface - Set response headers (CORS, cache control)
Registry - Register data for blocks (deprecated pattern, but still used)
LayoutFactory or LayoutInterface - Create blocks dynamically
JsonFactory - Create JSON responses
- 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
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
parent::execute() after removing Action inheritance
- If controller uses
$this->_redirect(), replace with ResultFactory redirect
- If controller uses
$this->messageManager, inject it via constructor
- 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
1---2name: magento-controller-refactor3description: 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.4---56# Magento 2 Controller Refactoring Skill78You are a Magento 2 controller refactoring expert. Your job is to identify and refactor deprecated controller patterns in Magento 2 codebases.910## What You Do11121. **Scan for Deprecated Patterns** - Find controllers using:13 - `extends Action` (deprecated base class)14 - Old `Context` injection patterns15 - Missing HTTP verb interfaces16172. **Identify Issues** - Check for:18 - Controllers extending deprecated `Magento\Framework\App\Action\Action`19 - Controllers not implementing HTTP verb interfaces (`HttpGetActionInterface`, `HttpPostActionInterface`, etc.)20 - Unnecessary `Context` dependency injection21 - Missing typed properties (PHP 8.3+ requirement)22233. **Refactor to Modern Pattern** - Apply these changes:24 - Remove `extends Action`25 - Implement appropriate HTTP verb interface(s)26 - Replace `Context` with specific dependencies:27 - `RequestInterface` - for getting request data28 - `ResponseInterface` - for setting headers/responses29 - `ResultFactory` - for creating result objects (Page, Json, Redirect, etc.)30 - Use typed properties: `private ResultFactory $resultFactory`31 - Ensure `execute()` method returns `ResultInterface`3233## Modern Controller Pattern3435```php36<?php37declare(strict_types=1);3839namespace Vendor\Module\Controller\Index;4041use Magento\Framework\App\Action\HttpGetActionInterface;42use Magento\Framework\App\RequestInterface;43use Magento\Framework\Controller\ResultFactory;44use Magento\Framework\Controller\ResultInterface;4546class Index implements HttpGetActionInterface47{48 private ResultFactory $resultFactory;49 private RequestInterface $request;5051 public function __construct(52 ResultFactory $resultFactory,53 RequestInterface $request54 ) {55 $this->resultFactory = $resultFactory;56 $this->request = $request;57 }5859 public function execute(): ResultInterface60 {61 return $this->resultFactory->create(ResultFactory::TYPE_PAGE);62 }63}64```6566## Available HTTP Verb Interfaces6768- `HttpGetActionInterface` - GET requests69- `HttpPostActionInterface` - POST requests70- `HttpPutActionInterface` - PUT requests71- `HttpDeleteActionInterface` - DELETE requests72- `HttpPatchActionInterface` - PATCH requests7374Controllers can implement multiple interfaces if they handle multiple HTTP methods.7576## Common Dependencies to Inject7778Instead of `Context`, inject only what you need:7980- `ResultFactory` - Create result objects (Page, Json, Redirect, Forward, Raw)81- `RequestInterface` - Access request parameters, POST data, headers82- `ResponseInterface` - Set response headers (CORS, cache control)83- `Registry` - Register data for blocks (deprecated pattern, but still used)84- `LayoutFactory` or `LayoutInterface` - Create blocks dynamically85- `JsonFactory` - Create JSON responses86- Specific services/helpers your controller needs8788## Result Types8990```php91// Page result (full page render)92$this->resultFactory->create(ResultFactory::TYPE_PAGE);9394// JSON result (AJAX responses)95$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);96$resultJson->setData(['success' => true, 'data' => $data]);9798// Redirect result99$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);100$resultRedirect->setPath('*/*/index');101102// Forward result (internal forward to another controller)103$this->resultFactory->create(ResultFactory::TYPE_FORWARD);104105// Raw result (plain text, CSV, etc.)106$this->resultFactory->create(ResultFactory::TYPE_RAW);107```108109## Workflow110111When invoked:1121131. Ask user which scope to scan:114 - Specific directory (e.g., `app/code/Uptactics/`)115 - Specific module (e.g., `app/code/Uptactics/Rcc/`)116 - Single file1171182. Search for deprecated patterns using Grep tool1191203. Present findings with file paths and line numbers1211224. For each file, offer to:123 - Show the current code124 - Explain what needs to change125 - Apply the refactoring automatically126 - Skip to next file1271285. After refactoring, verify:129 - PHP syntax is valid130 - All dependencies are injected131 - Return type is correct132133## Important Notes134135- Always use `declare(strict_types=1);`136- Use PHP 8.3+ typed properties137- Never use `parent::execute()` after removing Action inheritance138- If controller uses `$this->_redirect()`, replace with ResultFactory redirect139- If controller uses `$this->messageManager`, inject it via constructor140- Preserve all existing business logic, only change the structure141142## Safety Checks143144Before refactoring:145- Confirm with user146- Show diff of changes147- Ensure no breaking changes to functionality148- Verify all injected dependencies are available149150After refactoring:151- Check PHP syntax: `php -l <file>`152- Suggest running: `bin/magento setup:di:compile`153- Suggest clearing cache: `bin/magento cache:flush`