<?php
declare(strict_types=1);
namespace Module\Coord\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
/**
* Constructor
*
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Execute view action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
1 Answer 1
Deprecate the use of Magento\Framework\App\Action\Action and Magento\Framework\App\Action\Context, you can replace them with Magento\Framework\App\Action\HttpGetActionInterface (for GET actions) or Magento\Framework\App\Action\HttpPostActionInterface (for POST actions). This approach avoids inheriting the Action class and its constructor by defining the controller class as an implementation of HttpGetActionInterface or HttpPostActionInterface.
Updated Code
- Implement HttpGetActionInterface instead of extending Action.
- Remove the parent::__construct call.
Explanation
HttpGetActionInterface: Implements the appropriate interface rather than extending Action.
Removal of Context and parent::__construct call: The Context parameter is no longer needed, simplifying the constructor.
Code:
<?php
/**
* Copyright © 2024
* All rights reserved.
* See COPYING.txt for license details.
* Check module
* Magento 2.4.7-p3
*/
declare(strict_types=1);
namespace Module\Coord\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Index implements HttpGetActionInterface
*
* @class Index
* @since 11/13/2024
* @version 1.0.0
*/
class Index implements HttpGetActionInterface
{
/**
* Constructor
*
* @param PageFactory $resultPageFactory
*/
public function __construct(
protected PageFactory $resultPageFactory
){
}
/**
* Function execute view action
*
* @return ResultInterface
*/
public function execute(): ResultInterface
{
return $this->resultPageFactory->create();
}
}